Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15844 Accepted: 6135
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
Source
USACO 2003 February Green
题意:
无向图 ,从1走到n,再从n走回1 ,每一条边不能重复走,求最短距离
显然 求2遍最短路不能保证正确性
如果将边的距离看做费用,每条边容量为1,表示只能走一遍
源点S到1的容量为2,费用为0
n到汇点T容量为2,费用为0
那就转化为了最小费用流问题
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
const int N = 1e3 + 5;
const int M = 5e4;
struct Edge{
int to,c,w,next;
}edge[M];
int head[N];
int nume=0;
inline void addEdge(int k,int u,int v,int c,int w){
edge[k].to=v,
edge[k].c=c,
edge[k].w=w,
edge[k].next=head[u];
head[u]=k;
}
inline void addEdge(int u,int v,int c,int w){
addEdge(nume++,u,v,c,w);
addEdge(nume++,v,u,0,-w);
}
void init(int n){
fill(head,head+n+1,-1);
nume=0;
}
bool used[N];
int dis[N],load[N],p[N];//距离 ,前驱边,前驱点
bool spfa(int s,int e,int n){
deque<int>que;
for(int i=0;i<=n;++i){
dis[i]=INF;
load[i]=p[i]=-1;
used[i]=false;
}
que.push_back(s);
dis[s]=0;
used[s]=true;
while(!que.empty()){
int u=que.front();
que.pop_front();
used[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next){
if(edge[i].c>0){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].w){
dis[v]=dis[u]+edge[i].w;
p[v]=u;
load[v]=i;
if(used[v]==false){
used[v]=true;
que.push_back(v);
}
}
}
}
}
return dis[e]!=INF;
}
int min_cost_flow(int s,int t,int n){
int ansflow=0,anscost=0;
while(spfa(s,t,n)){
int u=t;
int f=INF;
while(p[u]!=-1){
f=min(f,edge[load[u]].c);
u=p[u];
}
u=t;
while(p[u]!=-1){
edge[load[u]].c-=f;
edge[load[u]^1].c+=f;
u=p[u];
}
anscost+=dis[t]*f;
ansflow+=f;
}
return anscost;
}
void slove(int n,int m){
int s=n+1,t=n+2;
init(n+2);
for(int i=1;i<=m;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,1,w);
addEdge(v,u,1,w);
}
addEdge(s,1,2,0);
addEdge(n,t,2,0);
printf("%d\n",min_cost_flow(s,t,n+2));
}
int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int n,m;
while(~scanf("%d%d",&n,&m)){
slove(n,m);
}
return 0;
}