POJ 2135 Farm Tour

来源:互联网 发布:windows端口初始化失败 编辑:程序博客网 时间:2024/05/16 09:13

Description

When FJ's friendsvisit 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 houseand the Nth of which contains the big barn. A total M (1 <= M <= 10000) pathsthat connect the fields in various ways. Each path connects two differentfields 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, hereturns (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 walkon any given path more than once. Calculate the shortest tour possible. FJ issure that some tour exists for any given farm.

Input

* Line 1: Twospace-separated integers: N and M. 

* Lines 2..M+1: Three space-separated integers that define a path: The startingfield, the end field, and the path's length.
 

Output

A single linecontaining 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<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#define INF 1000000000using namespace std;const int M = 4 * 10010;//边struct Node//边,点f到点t,流量为c,费用为w{int st, ed;int flow, cost;int next;}edge[M];int head[M], dis[M], q[M], pre[M], cnt;//cnt为已添加的边数,head为邻接表,dis为花费,pre为父亲节点bool vis[M];int n ,m;//n个点,m条路void init(){memset(head, -1, sizeof(head));cnt = 0;}void add_edge(int f, int t, int d1, int d2, int w){//f到t的一条边,流量为d1,反向流量d2,花费w,反向边花费-w(可以反悔)edge[cnt].st = f;edge[cnt].ed = t;edge[cnt].flow = d1;edge[cnt].cost = w;edge[cnt].next = head[f];head[f] = cnt++;edge[cnt].st = t;edge[cnt].ed = f;edge[cnt].flow = d2;edge[cnt].cost = -w;edge[cnt].next = head[t];head[t] = cnt++;}bool spfa(int s, int t, int n){int i, tmp, l, r;memset(pre, -1, sizeof(pre));for(i = 0; i < n; ++i)    {        dis[i] = INF;    }dis[s] = 0;q[0] = s;l = 0, r = 1;vis[s] = true;while(l != r)    {tmp = q[l];l = (l + 1) % (n + 1);vis[tmp] = false;for(i = head[tmp]; i!=-1; i = edge[i].next){if(edge[i].flow && dis[edge[i].ed] > dis[tmp] + edge[i].cost){dis[edge[i].ed] = dis[tmp] + edge[i].cost;pre[edge[i].ed] = i;if(!vis[edge[i].ed]){vis[edge[i].ed] = true;q[r] = edge[i].ed;r = (r + 1) % (n + 1);}}}}if(pre[t] == -1){    return false;}return true;}void MCMF(int s, int t, int n, int &flow, int &cost){//起点s,终点t,点数n,最大流flow,最小花费costint tmp, arg;flow = cost = 0;while(spfa(s, t, n))    {arg = INF;tmp = t;while(tmp != s){arg = min(arg, edge[pre[tmp]].flow);tmp = edge[pre[tmp]].st;}tmp = t;while(tmp != s){edge[pre[tmp]].flow -= arg;edge[pre[tmp] ^ 1].flow += arg;tmp = edge[pre[tmp]].st;}flow += arg;cost += arg * dis[t];}}int main(){int st ,ed ,co;int flow ,cost;while(~scanf("%d%d",&n,&m)){init();for(int i = 0;i < m;i++){scanf("%d%d%d",&st,&ed,&co);add_edge(st,ed,1,0,co);add_edge(ed,st,1,0,co);}add_edge(0,1,2,0,0);add_edge(n,n+1,2,0,0);MCMF(0,n+1,n+2,flow,cost);printf("%d\n",cost);}return 0;}


0 0