【POJ】 2135 Farm Tour 费用流
来源:互联网 发布:linux给文件夹重命名 编辑:程序博客网 时间:2024/06/01 09:20
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.
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
USACO 2003 February Green
题目大意:n个点,m条无向边,起点始终为1,终点始终为n,问从起点走到终点再从终点走到起点经过的最短路径是多少。
题目分析:将起点走到终点再走回起点转化为两次从起点走到终点且不走重复边,建立超级源汇连接起点和终点,容量为2,费用为1,其他按照题目建边跑一遍最小费用流即可。
代码如下:
- Time Limit: 1000MS
- Memory Limit: 65536K
- Total Submissions: 11005
- Accepted: 4072
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 51 2 12 3 13 4 11 3 22 4 2
Sample Output6
SourceUSACO 2003 February Green
题目大意:n个点,m条无向边,起点始终为1,终点始终为n,问从起点走到终点再从终点走到起点经过的最短路径是多少。
题目分析:将起点走到终点再走回起点转化为两次从起点走到终点且不走重复边,建立超级源汇连接起点和终点,容量为2,费用为1,其他按照题目建边跑一遍最小费用流即可。
代码如下:
#include <stdio.h>#include <string.h>#include <memory.h>#define min(a, b) ((a) < (b) ? (a) : (b))#define REP(i, n) for(int i = 0; i < n; ++i)const int maxE = 1000000;const int maxN = 1005;const int oo = 0x3f3f3f3f;struct Edge{ int v, c, w, n;};Edge edge[maxE];int adj[maxN], l;int d[maxN], cur[maxN], a[maxN];int inq[maxN], Q[maxE], head, tail;int n, m;int cost, flow, s, t;void addedge(int u, int v, int c, int w){ edge[l].v = v; edge[l].c = c; edge[l].w = w; edge[l].n = adj[u]; adj[u] = l++; edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;}int SPFA(){ memset(d, oo, sizeof d); memset(inq, 0, sizeof inq); head = tail = 0; d[s] = 0; a[s] = oo; cur[s] = -1; Q[tail++] = s; while(head != tail){ int u = Q[head++]; inq[u] = 0; for(int i = adj[u]; ~i; i = edge[i].n){ int v = edge[i].v; if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue; d[v] = d[u] + edge[i].w; cur[v] = i; a[v] = min(edge[i].c, a[u]); if(inq[v]) continue; inq[v] = 1; Q[tail++] = v; } } if(d[t] == oo) return 0; flow += a[t]; cost += a[t] * d[t]; for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){ edge[i].c -= a[t]; edge[i ^ 1].c += a[t]; } return 1;}int MCMF(){ flow = cost = 0; while(SPFA()); return cost;}void work(){ int u, v, w; memset(adj, -1, sizeof adj); l = 0; s = 0; t = n + 1; while(m--){ scanf("%d%d%d", &u, &v, &w); addedge(u, v, 1, w); addedge(v, u, 1, w); } addedge(s, 1, 2, 0); addedge(n, t, 2, 0); printf("%d\n", MCMF());}int main(){ while(~scanf("%d%d", &n, &m)) work(); return 0;}
0 0
- POJ 2135 Farm Tour(最小费用流)
- POJ 2135 Farm Tour(最小费用流)
- POJ 2135 Farm Tour (费用流)
- POJ 2135 Farm Tour 最小费用流
- poj 2135 Farm Tour 费用流
- 【POJ】 2135 Farm Tour 费用流
- POJ 2135 Farm Tour | 费用流
- POJ 2135 Farm Tour 费用流
- POJ 2135 Farm Tour(费用流)
- [POJ 2135] Farm Tour · 费用流
- POJ 2135 Farm Tour【最小费用流】
- poj 2135 Farm Tour(最小费用流)
- POJ 2135 Farm Tour(费用流)
- poj 2135 Farm Tour 最小费用流
- poj 2135 Farm Tour 费用流
- poj 2135 Farm Tour 【最小费用流】
- POJ 2135 Farm Tour 最小费用流
- POJ-2135 Farm Tour(费用流)
- 【HDU】 4832 Chess 排列组合 DP
- 【UVa】 10735 Euler Circuit 混合图的欧拉回路 最大流
- 【HDU】 3395 Special Fish 费用流(可KM匹配)
- 【HDU】 2448 Mining Station on the Sea 费用流
- 【POJ】 1986 Distance Queries 离线LCA
- 【POJ】 2135 Farm Tour 费用流
- 【POJ】 2516 Minimum Cost 费用流
- 【HDU】 4067 Random Maze 费用流
- 【POJ】 3422 Kaka’s Matrix Travels 费用流
- 【POJ】 3680 Intervals 离散 + 费用流
- 【POJ】 3762 The Bonus Salary! 离散 + 费用流
- Java学习之路之“我的第一个Java程序“HelloWorld””
- android 支付宝之网页支付和快捷支付
- Android: Bundle