题目链接  

 

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18961   Accepted: 7326

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.

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. 

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条边,无向图,问你1->n->1的最短路径是多少。FJ带朋友参观,想经可能多看几个地方,所以每条边只能走一次。

思路:把路径长度作为费用,流量为1,这样每条路就只能走一次了。自己建源点s,汇点t。s->1流量2,n->t流量2,因为这两条边要走两次。然后直接费用流。水题,改改板子就过了,学会费用流后的第二题。

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <queue>
  6 
  7 using namespace std;
  8 const int MAXN = 5010;
  9 const int MAXM = 50010;
 10 const int INF = 0x7FFFFFFF;
 11 
 12 int n, m, first[MAXN], s, t, sign;
 13 
 14 int max_flow, min_cost;
 15 
 16 struct Edge {
 17     int to, cap, cost, next;
 18 } edge[MAXM * 2];
 19 
 20 inline void init() {
 21     for(int i = 0; i <= n + 1; i++ ) {
 22         first[i] = -1;
 23     }
 24     sign = 0;
 25 }
 26 
 27 inline void add_edge(int u, int v, int cap, int cost) {
 28     edge[sign].to = v, edge[sign].cap = cap, edge[sign].cost = cost;
 29     edge[sign].next = first[u], first[u] = sign ++;
 30     edge[sign].to = u, edge[sign].cap = 0, edge[sign].cost = -cost;
 31     edge[sign].next = first[v], first[v] = sign ++;
 32 }
 33 
 34 int dist[MAXN], inq[MAXN], pre[MAXN], incf[MAXN];
 35 
 36 bool spfa(int s, int t) {
 37     for(int i = 0; i <= n + 1; i++ ) {
 38         dist[i] = INF, inq[i] = 0;
 39     }
 40     queue<int>que;
 41     que.push(s), inq[s] = 1, dist[s] = 0;
 42     incf[s] = 0x3f3f3f3f;
 43     while(!que.empty()) {
 44         int now = que.front();
 45         que.pop();
 46         inq[now] = 0;
 47         for(int i = first[now]; ~i; i = edge[i].next) {
 48             int to = edge[i].to, cap = edge[i].cap, cost = edge[i].cost;
 49             if(cap > 0 && dist[to] > dist[now] + cost) {
 50                 dist[to] = dist[now] + cost;
 51                 incf[to] = min(incf[now], cap);
 52                 pre[to] = i;
 53                 if(!inq[to]) {
 54                     que.push(to);
 55                     inq[to] = 1;
 56                 }
 57             }
 58         }
 59     }
 60     return dist[t] != INF;
 61 }
 62 
 63 void update(int s, int t) {
 64     int x = t;
 65     while(x != s) {
 66         int pos = pre[x];
 67         edge[pos].cap -= incf[t];
 68         edge[pos ^ 1].cap += incf[t];
 69         x = edge[pos ^ 1].to;
 70     }
 71     max_flow += incf[t];
 72     min_cost += dist[t] * incf[t];
 73 }
 74 
 75 void minCostMaxFlow(int s, int t) {
 76     while(spfa(s, t)) {
 77         update(s, t);
 78     }
 79 }
 80 
 81 int main()
 82 {
 83     while(~scanf("%d %d", &n, &m)) {
 84         s = 0, t = n + 1;
 85         init();
 86         for(int i = 1; i <= m; i++ ) {
 87             int u, v, cap, cost;
 88             scanf("%d %d %d", &u, &v, &cost);
 89             add_edge(u, v, 1, cost);
 90             add_edge(v, u, 1, cost);
 91         }
 92         add_edge(s, 1, 2, 0);
 93         add_edge(n, t, 2, 0);
 94         max_flow = min_cost = 0;
 95         minCostMaxFlow(s, t);
 96         printf("%d\n", min_cost);
 97     }
 98 
 99     return 0;
100 }