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
题目大意
约翰家有 N 间牛棚,M 条双向道路连接了这些牛棚,第 i 条道路连接了第 A i 间牛棚和第 B i 间牛棚,长度为 L i 。所有牛棚中最好的是第一间和最后一间,所以当有朋友来访时,他会带着朋友从第一间牛棚走到第 N 间牛棚,然后再回到第一间牛棚。约翰想让朋友多看看乡村不同的景色,所以希望来回的路上不重复经过任何一条道路,不过重复经过一间牛棚是允许的。请帮助约翰选择一条路线,使得往返路径的总长度最短。输入数据保证路线总是存在的。
题解
就是流量为$2$的最小费用流。
每条边流量为$1$,建立原点连向1号节点,流量为$2$。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int N=;
const int M=; int n,m,u,v,c;
struct tt
{
int to,next,cost,cap;
}edge[M*+];
int path[N+],top=-;
int pre[N+]; void Add(int u,int v,int cost,int cap)
{
edge[++top].to=v;
edge[top].next=path[u];
edge[top].cost=cost;
edge[top].cap=cap;
path[u]=top;
} int SPFA()
{
int dist[N+];
memset(dist,/,sizeof(dist));dist[]=;
int INF=dist[];
bool vis[N+]={};vis[]=;
queue<int>Q;
while (!Q.empty()) Q.pop();
Q.push();
while (!Q.empty())
{
int u=Q.front();Q.pop();vis[u]=;
for (int i=path[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if (dist[v]>dist[u]+edge[i].cost&&edge[i].cap>)
{
dist[v]=dist[u]+edge[i].cost;
pre[v]=i;
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
return dist[n]==INF ? :dist[n];
} void change(int r)
{
if (!r) return;
edge[pre[r]].cap--;
edge[pre[r]^].cap++;
change(edge[pre[r]^].to);
} int min_cost_flow()
{
int tolcost=;
int tmp;
while (tmp=SPFA()) tolcost+=tmp,change(n);
return tolcost;
} int main()
{
memset(path,-,sizeof(path));
scanf("%d%d",&n,&m);
Add(,,,);
Add(,,,);
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
Add(u,v,c,);
Add(v,u,-c,);
Add(v,u,c,);
Add(u,v,-c,);
}
printf("%d\n",min_cost_flow());
return ;
}