Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14530 | Accepted: 5540 |
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
解题思路:最小费用最大流的模板题。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
struct Edge
{
int from,to,next,flow,cost;
}edge[maxn*maxn];
int n,m,cnt,head[maxn],pre[maxn];
int dis[maxn],st,ed;
bool inq[maxn];
void addedge(int u,int v,int flow,int cost)
{
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].flow = flow;
edge[cnt].cost = cost;
edge[cnt].next = head[u];
head[u] = cnt++;
swap(u,v);
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].flow = 0;
edge[cnt].cost = -cost;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int spfa(int s,int t)
{
queue<int> q;
memset(dis,inf,sizeof(dis));
memset(inq,false,sizeof(inq));
memset(pre,-1,sizeof(pre)); //pre[i]表示最短路径上以i为终点的边的编号
dis[s] = 0;
inq[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
inq[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].cost && edge[i].flow > 0)
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(inq[v] == false)
{
inq[v] = true;
q.push(v);
}
}
}
}
return dis[t] != inf;
}
int MCMF(int s,int t)
{
int mincost = 0,minflow,sumflow = 0; //最小费用,路径中最小流量,总流量
while(spfa(s,t)) //找当前的最短路
{
minflow = inf;
for(int i = pre[t]; i != -1; i = pre[edge[i].from])
minflow = min(minflow,edge[i].flow);
sumflow += minflow;
for(int i = pre[t]; i != -1; i = pre[edge[i].from])
{
edge[i].flow -= minflow;
edge[i^1].flow += minflow;
}
mincost += dis[t] * minflow;
}
return mincost;
}
int main()
{
int u,v,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt = 0;
memset(head,-1,sizeof(head));
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,1,c);
addedge(v,u,1,c);
}
st = 0, ed = n + 1;
addedge(st,1,2,0); //有来回,所以容量为2
addedge(n,ed,2,0);
printf("%d\n",MCMF(st,ed));
}
return 0;
}