解题报告
题意:
一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路。
思路:
如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了。
一次mcmf模版。
#include <iostream> #include <cstring> #include <queue> #include <cstdio> #define inf 0x3f3f3f3f using namespace std; int head[2010],dis[2010],vis[2010],pre[2010],f[2010],cnt,flow,cost,s,t,n,m; struct node { int v,cap,cost,next; } edge[50000]; void add(int u,int v,int cost,int cap) { edge[cnt].v=v,edge[cnt].cost=cost,edge[cnt].cap=cap; edge[cnt].next=head[u],head[u]=cnt++; edge[cnt].v=u,edge[cnt].cost=-cost,edge[cnt].cap=0; edge[cnt].next=head[v],head[v]=cnt++; } int _spfa() { queue<int>Q; Q.push(s); for(int i=1; i<=n; i++)dis[i]=inf,pre[i]=vis[i]=f[i]=0; dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf; while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) { pre[v]=i; f[v]=min(edge[i].cap,f[u]); dis[v]=dis[u]+edge[i].cost; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } if(dis[t]==inf)return 0; flow+=f[t]; cost+=f[t]*dis[t]; if(flow==2)return 0; for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) { edge[i].cap-=f[t]; edge[i^1].cap+=f[t]; } return 1; } void mcmf() { cost=flow=0; while(_spfa()); printf("%d\n",cost); } int main() { int u,v,w; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); s=1; t=n; while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w,1); add(v,u,w,1); } mcmf(); return 0; }
Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11402 | Accepted: 4230 |
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.
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 5 1 2 1 2 3 1 3 4 1 1 3 2 2 4 2
Sample Output
6
POJ2135_Farm Tour(网络流/费用流)