| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17603 | Accepted: 6808 |
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
Source
这一题建模是关键。如果我们刷两次dij什么会很麻烦,而且容易些挂。我们进而想想——我们只不过就是找两条不相重的路,我们怎么限制每条路只走一次呢?对,用流!
我们建一个超级源汇,相当于从源到汇有流量为2的流,而每条边的容量都是1(当然除了超级源与超级汇对应的边)。建模以后,我们就可以跑MCMF-spfa了(第一次写qwq)。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 const int N=1005,M=40010; 7 int n,m,dis[N],pre[N]; 8 int tot,nxt[M],son[M],w[M],cap[M],top[M],lnk[N]; 9 bool vis[N]; 10 void add(int x,int y,int co,int fl){ 11 nxt[++tot]=lnk[x],son[tot]=y,w[tot]=co,cap[tot]=fl,top[tot]=x,lnk[x]=tot; 12 nxt[++tot]=lnk[y],son[tot]=x,w[tot]=-co,cap[tot]=0,top[tot]=y,lnk[y]=tot; 13 } 14 bool spfa(){ 15 memset(pre,255,sizeof pre); 16 memset(dis,63,sizeof dis),dis[0]=0; 17 memset(vis,0,sizeof vis),vis[0]=1; 18 int x; queue <int> Q; while (!Q.empty()) Q.pop(); Q.push(0); 19 while (!Q.empty()){ 20 x=Q.front(),vis[x]=0,Q.pop(); 21 for (int j=lnk[x]; j; j=nxt[j]) 22 if (cap[j]>0&&dis[son[j]]>dis[x]+w[j]){ 23 dis[son[j]]=dis[x]+w[j],pre[son[j]]=j; 24 if (!vis[son[j]]) vis[son[j]]=1,Q.push(son[j]); 25 } 26 } 27 return pre[n+1]!=-1; 28 } 29 int MCMF(){ 30 int ret=0; 31 while (spfa()){ 32 int fl=dis[N-1]; 33 for (int j=pre[n+1]; j+1; j=pre[top[j]]) 34 if (fl>cap[j]) fl=cap[j]; 35 for (int j=pre[n+1]; j+1; j=pre[top[j]]) 36 ret+=fl*w[j],cap[j]-=fl,cap[j^1]+=fl; 37 } 38 return ret; 39 } 40 int main(){ 41 scanf("%d%d",&n,&m); int x,y,z; tot=1; 42 for (int i=1; i<=m; i++) 43 scanf("%d%d%d",&x,&y,&z),add(x,y,z,1),add(y,x,z,1); 44 add(0,1,0,2),add(n,n+1,0,2); 45 printf("%d",MCMF()); 46 return 0; 47 }
本文介绍了一种利用最大流最小费用算法求解农场中从家到谷仓再返回的最短双路径问题的方法。通过建立超级源汇模型,将问题转化为寻找两条不重合路径的最短总距离,使用MCMF-spfa算法实现。代码示例展示了具体实现过程。
2530

被折叠的 条评论
为什么被折叠?



