每一条路只能走一次,求一条起点到终点再到起点最短的路。那么给每一条路赋值容量为1,费用是距离,从起点开始用最小费用流增广2次后的费用就是最小费用。这题就是最小费用最大流。
Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7904 | Accepted: 2837 |
Description
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.
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.
* 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 5 1 2 1 2 3 1 3 4 1 1 3 2 2 4 2
Sample Output
6#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<cstring> using namespace std; #define MAXN 11000 #define MAXM 101000 #define INF 0x3FFFFFF //此处应该开大点 //通常我开0xFFFFFF这次貌似小了 struct edge { int to,c,w,next; }; edge e[MAXM]; bool in[MAXN]; int head[MAXN],dis[MAXN],pre[MAXN],en,maxflow,mincost; int vn,st,ed,n,m; void add(int a,int b,int c,int d) { e[en].to=b; e[en].c=c; e[en].w=d; e[en].next=head[a]; head[a]=en++; e[en].to=a; e[en].c=0; e[en].w=-d; e[en].next=head[b]; head[b]=en++; } bool spfa(int s) { queue<int> q; for(int i=0;i<=vn;i++) { dis[i]=INF; in[i]=false; pre[i]=-1; } dis[s]=0; in[s]=true; q.push(s); while(!q.empty()) { int tag=q.front(); in[tag]=false; q.pop(); for(int i=head[tag];i!=-1;i=e[i].next) { int j=e[i].to; if(e[i].w+dis[tag]<dis[j] && e[i].c) { dis[j]=e[i].w+dis[tag]; pre[j]=i; if(!in[j]) { q.push(j); in[j]=true; } } } } if(dis[ed]==INF) return false; return true; } void update() { int flow=INF; for (int i=pre[ed];i!=-1;i=pre[e[i^1].to]) if(e[i].c<flow) flow=e[i].c; for (int i=pre[ed];i!=-1;i=pre[e[i^1].to]) { e[i].c-=flow,e[i^1].c+=flow; } maxflow+=flow; mincost+=flow*dis[ed]; } void mincostmaxflow() { maxflow=0,mincost=0; while(spfa(st)) update(); } void solve() { int a,b,c; en=0,st=1,ed=n,vn=n+2; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,1,c); //无向图 add(b,a,1,c); } maxflow=0,mincost=0; spfa(st); update(); spfa(st); update(); printf("%d\n",mincost); } int main() { while(scanf("%d%d",&n,&m)!=EOF) solve(); return 0; }