POJ - 2135 Farm Tour (最小费用最大流)
Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18973 | Accepted: 7332 |
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
这道题就是让主角找到从1到n的两条线路并且让他们的长度和最短~~;
刚开始看并没有什么思路~~可是你们仔细一想~让一个图边容量为1~找到最小的花费~~不就是最小费用的最大流吗~~我们建立一个超级源点0~链接1~让边容量为2~边权为0~然后求从0到n的最小费用最大流就好~
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include<cstring>
using namespace std;
const int maxn=2005;
const int maxm=20005;
#define inf 0x7fffffff
struct ***
{
int to,ne,cap,flow,cost;
} ed[maxm*2];
int head[maxn],cnt;
int pre[maxm*2],dis[maxn];
bool vis[maxn];
int n,m;
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int cap,int cost)
{
ed[cnt].to=v;
ed[cnt].cap=cap;
ed[cnt].cost=cost;
ed[cnt].flow=0;
ed[cnt].ne=head[u];
head[u]=cnt++;
ed[cnt].to=u;
ed[cnt].cap=0;
ed[cnt].cost=-cost;
ed[cnt].flow=0;
ed[cnt].ne=head[v];
head[v]=cnt++;
}
bool spfa(int s, int t)
{
queue<int>q;
for(int s=0;s<=n+1;s++)
{
dis[s]=inf;
vis[s]=0;
}
memset(pre,-1,sizeof(pre));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int s=head[u];~s;s=ed[s].ne)
{
int v=ed[s].to;
// cout<<u<<" "<<v<<" "<<ed[s].cost<<endl;
if(ed[s].cap>ed[s].flow&&dis[v]>dis[u]+ed[s].cost)
{
dis[v]=dis[u]+ed[s].cost;
pre[v]=s;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(pre[t]==-1)
{
return 0;
}
return 1;
}
int Mincost_Maxflow(int s,int t,int &cost)
{
int flow=0;
cost=0;
while(spfa(s,t))
{
int Min=inf;
for(int s=pre[t];~s;s=pre[ed[s^1].to])
{
Min=min(Min,(ed[s].cap-ed[s].flow));
}
for(int s=pre[t];~s;s=pre[ed[s^1].to])
{
ed[s].flow+=Min;
ed[s^1].flow-=Min;
cost+=ed[s].cost*Min;
}
flow+=Min;
}
return flow;
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int s=0;s<m;s++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,1,c);
add(b,a,1,c);
}
add(0,1,2,0);
add(n,n+1,2,0);
int cost;
int k=Mincost_Maxflow(0,n+1,cost);
cout<<cost<<endl;
return 0;
}