首页 > 代码库 > Farm Tour(网络流模板题)
Farm Tour(网络流模板题)
题目描述
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.
输入
* 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.
输出
A single line
containing the length of the shortest tour.
样例输入
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
样例输出
6
#include<cstdio> #include<cstring> struct edge{ int to,cap,rev,nx,we; }G[40050]; int n,m,p; int h[2050],q[40050],d[2050]; bool mark[2050]; int Min(int a,int b){return a<b?a:b;} void ae(int s,int e,int c,int w){ G[++p]=(edge){e,c,p+1,h[s],w};h[s]=p; G[++p]=(edge){s,0,p-1,h[e],-w};h[e]=p; } bool spfa(){ memset(d,127/3,sizeof(d)); memset(mark,0,sizeof(mark)); d[n+1]=0;mark[n+1]=1; int head=0,tail=0; int inf=d[0]; q[tail++]=n+1; while(head!=tail){ int fr=q[head++]; for(int i=h[fr];i;i=G[i].nx){ if(G[G[i].rev].cap>0&&d[G[i].to]>d[fr]+G[G[i].rev].we){ d[G[i].to]=d[fr]+G[G[i].rev].we; if(!mark[G[i].to])mark[G[i].to]=1,q[tail++]=G[i].to; } } mark[fr]=0; } return !(d[0]==inf); } int dfs(int s,int t,int f){ mark[s]=1; if(s==t) return f; int sum=0; for(int i=h[s];i;i=G[i].nx){ if(G[i].cap>0&&!mark[G[i].to]&&d[s]-G[i].we==d[G[i].to]){ int d=dfs(G[i].to,t,Min(G[i].cap,f)); if(d)sum+=d,f-=d,G[i].cap-=d,G[G[i].rev].cap+=d; if(f==0)return sum; } } return sum; } int m__f(){ int sum=0; while(spfa()){ mark[n+1]=1; while(mark[n+1]){ memset(mark,0,sizeof(mark)); sum+=dfs(0,n+1,99999999)*d[0]; } } return sum; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); ae(x,y,1,z);ae(y,x,1,z); } ae(0,1,2,0);ae(n,n+1,2,0); printf("%d\n",m__f()); return 0; }
Farm Tour(网络流模板题)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。