码迷,mamicode.com
首页 > 其他好文 > 详细

[网络流]Farm Tour(费用流

时间:2018-04-18 20:12:52      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:--   oid   题目   init   最大   ISE   col   enum   nes   

Farm Tour

 

题目描述

 

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.

 

输入

 

* 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.

 

输出

 

A single line containing the length of the shortest tour.

 

 

样例输入

 

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

 

样例输出

 

6



代码:
  1 #include<iostream> 
  2 #include<queue> 
  3 #include<cstdio>
  4 #include<cstring>
  5 using namespace std; 
  6 int total; 
  7 const int MAXN = 1010; 
  8 const int INF = 1000000000; 
  9 struct Edge 
 10 { 
 11     int u, v, cap, cost; 
 12     int next; 
 13 } edge[40010]; 
 14 int edgenum; 
 15 int head[MAXN], dist[MAXN], pre[MAXN]; 
 16 bool vis[MAXN]; 
 17 void init() 
 18 { 
 19     edgenum = 0; 
 20     memset(head, -1, sizeof(head)); 
 21 } 
 22 void addedge(int u, int v, int cap, int cost) 
 23 { 
 24     edge[edgenum].u = u; 
 25     edge[edgenum].v = v; 
 26     edge[edgenum].cap = cap; 
 27     edge[edgenum].cost = cost; 
 28     edge[edgenum].next = head[u]; 
 29     head[u] = edgenum++; 
 30     edge[edgenum].u = v; 
 31     edge[edgenum].v = u; 
 32     edge[edgenum].cap = 0; 
 33     edge[edgenum].cost = -cost; 
 34     edge[edgenum].next = head[v]; 
 35     head[v] = edgenum++; 
 36 } 
 37 bool spfa(int s, int t, int n)//找到一条增广路 
 38 { 
 39     int i, u, v; 
 40     queue <int> qu; 
 41     memset(vis, false, sizeof(vis)); 
 42     memset(pre, -1, sizeof(pre)); 
 43     for(i = 0; i <= n; i++) dist[i] = INF; 
 44     vis[s] = true; 
 45     dist[s] = 0; 
 46     qu.push(s); 
 47     while(!qu.empty()) 
 48     { 
 49         u = qu.front(); 
 50         qu.pop(); 
 51         vis[u] = false; 
 52         for(i = head[u]; i != -1; i = edge[i].next) 
 53         { 
 54             v = edge[i].v; 
 55             if(edge[i].cap && dist[v] > dist[u] + edge[i].cost) 
 56             { 
 57                 dist[v] = dist[u] + edge[i].cost; 
 58                 pre[v] = i; 
 59                 if(!vis[v]) 
 60                 { 
 61                     qu.push(v); 
 62                     vis[v] = true; 
 63                 } 
 64             } 
 65         } 
 66     } 
 67     if(dist[t] == INF) return false; 
 68     return true; 
 69 } 
 70 int min_cost_max_flow(int s, int t, int n) 
 71 { 
 72     int flow = 0; // 总流量 
 73     int i, minflow, mincost; 
 74     mincost = 0; 
 75     while(spfa(s, t, n)) 
 76     { 
 77         minflow = INF + 1; 
 78         for(i = pre[t]; i != -1; i = pre[edge[i].u]) 
 79             if(edge[i].cap < minflow) 
 80                 minflow = edge[i].cap; 
 81         flow += minflow; 
 82         for(i = pre[t]; i != -1; i = pre[edge[i].u]) 
 83         { 
 84             edge[i].cap -= minflow; 
 85             edge[i^1].cap += minflow; 
 86         } 
 87         mincost += dist[t] * minflow; 
 88     } 
 89     total = flow; // 最大流 
 90     return mincost; 
 91 } 
 92 int main() 
 93 { 
 94     int n, m; 
 95     int u, v, c; 
 96     while (scanf("%d%d", &n, &m) != -1) 
 97     { 
 98         init(); 
 99         int s = 0; 
100         int t = n + 1; 
101         while (m--) 
102         { 
103             scanf("%d%d%d", &u, &v, &c); 
104             addedge(u, v , 1 , c); 
105             addedge(v, u , 1 , c); 
106         } 
107         addedge(s, 1, 2, 0); 
108         addedge(n, t, 2, 0); 
109         int ans = min_cost_max_flow(s, t, n + 2); 
110         printf("%d\n", ans); 
111     } 
112     return 0; 
113 } 

 

[网络流]Farm Tour(费用流

标签:--   oid   题目   init   最大   ISE   col   enum   nes   

原文地址:https://www.cnblogs.com/GldHkkowo/p/8877032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!