Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10053 | Accepted: 3711 |
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点,每条边最多走一次,问最短需要走多远。
【分析】:
求最小费用最大流,代码为临接链表实现
【代码】:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define MAXN 1010
#define MAXM 1000001
#define IMAX 214748364
struct EDGE{int f,t,flow,cost,next;};
EDGE a[2*MAXM];
int N,M,last[MAXN],tot=0,dist[MAXN],ans=0,fa[MAXN];
bool vis[MAXN];
void add(int from,int to,int flow,int cost)
{
a[tot].t=to;
a[tot].f=from;
a[tot].next=last[from];
a[tot].flow=flow;
a[tot].cost=cost;
last[from]=tot++;
a[tot].t=from;
a[tot].f=to;
a[tot].next=last[to];
a[tot].flow=0;
a[tot].cost=-1*cost;
last[to]=tot++;
}
bool spfa()
{
queue<int> Q;
for(int i=0;i<=N+1;i++)
dist[i]=IMAX;
memset(vis,false,sizeof(vis));
memset(fa,-1,sizeof(fa));
dist[0]=0;
vis[0]=true;
Q.push(0);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=last[now];i!=-1;i=a[i].next)
{
int to=a[i].t;
if(a[i].flow && dist[now]+a[i].cost<dist[to])
{
dist[to]=dist[now]+a[i].cost;
fa[to]=i;
if(!vis[to])
{
vis[to]=true;
Q.push(to);
}
}
}
vis[now]=false;
}
if(dist[N+1]==IMAX) return false;
return true;
}
void MCMF()
{
while(spfa())
{
int minflow=IMAX;
for(int i=fa[N+1];i!=-1;i=fa[a[i].f])
minflow=a[i].flow<minflow?a[i].flow:minflow;
for(int i=fa[N+1];i!=-1;i=fa[a[i].f])
{
a[i].flow-=minflow;
a[i^1].flow+=minflow;
}
ans+=minflow*dist[N+1];
}
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(last,-1,sizeof(last));
tot=0;
for(int i=1;i<=M;i++)
{
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);
MCMF();
printf("%d\n",ans);
}
//system("pause");
return 0;
}