Farm Tour POJ

来源:互联网 发布:芜湖网络推广公司 编辑:程序博客网 时间:2024/06/05 07:08
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
Input
* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
A single line containing the length of the shortest tour.
Sample Input
4 51 2 12 3 13 4 11 3 22 4 2
Sample Output

#include <cstdio>#include <queue>#define min(a,b) (a>b?b:a)#define MAXV 1100#define MAXM 40100#include<string.h>using namespace std;typedef struct{    int s,t,next,w,r;} Edge;Edge edge[MAXM];int source,sink,n,m,mincost,edgesum;int head[MAXV],d[MAXV],parent[MAXV],M[MAXV];void addedge(int a,int b,int c,int r){    edge[edgesum].s=a;    edge[edgesum].t=b;    edge[edgesum].r=r;    edge[edgesum].w=c;    edge[edgesum].next=head[a];    head[a]=edgesum++;    edge[edgesum].s=b;    edge[edgesum].t=a;    edge[edgesum].r=0;    edge[edgesum].w=-c;    edge[edgesum].next=head[b];    head[b]=edgesum++;}int spfa(){    queue <int>q;    int v,i,tmp;    bool vis[MAXV];    for(i=0; i<=sink; i++) d[i]=0x3f3f3f3f;    memset(vis,false,sizeof(vis));    memset(parent,-1,sizeof(parent));    memset(M,0x3f3f3f3f,sizeof(M));    q.push(source);    vis[source]=true;    d[source]=0;    while(!q.empty())    {        v=q.front();        q.pop();        vis[v]=false;        for(i=head[v]; i!=-1; i=edge[i].next)        {            tmp=edge[i].t;            if(edge[i].r && edge[i].w+d[v]<d[tmp])            {                d[tmp]=edge[i].w+d[v];                parent[tmp]=i;                M[tmp]=min(M[edge[i].s],edge[i].r);                if(!vis[tmp])                {                    q.push(tmp);                    vis[tmp]=true;                }            }        }    }    return 0;}void MCMF(){    int u;    mincost=0;    while(1)    {        spfa();        if(parent[sink]==-1) break;        u=parent[sink];//这里不用求出增光路径上的最小流量        while(u!=-1) //因为最小流量一定为1        {            edge[u].r-=M[sink];            edge[u^1].r+=M[sink];            u=parent[edge[u].s];        }        mincost+=d[sink];    }}int main(){    int i,a,b,c;    while(~scanf("%d%d",&n,&m))    {        memset(head,-1,sizeof(head));        edgesum=0,source=0,sink=n+1;        for(i=1; i<=m; i++)        {            scanf("%d%d%d",&a,&b,&c);            addedge(a,b,c,1);            addedge(b,a,c,1);        }        addedge(source,1,0,2);        addedge(n,sink,0,2);        MCMF();        printf("%d\n",mincost);    }    return 0;}


原创粉丝点击