poj 2135 最小费用最大流模板题
2015-08-24 18:27
411 查看
Farm Tour
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
Sample Output
[/code]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13459 | Accepted: 5098 |
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
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan #define clr(s,x) memset(s,x,sizeof(s)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lowbit(x) (x&(-x)) #define PB push_back #define For(i,a,b) for(int i=a;i<b;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) typedef long long LL; typedef unsigned int uint; typedef unsigned long long ULL; typedef vector<int> vint; typedef vector<string> vstring; void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x; } void RII (int& x, int& y){RI (x), RI (y);} void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);} const double PI = acos(-1.0); const int maxn = 1e4 + 100; const int maxm = 1e3 + 100; const LL mod = 1e9 + 7; const double eps = 1e-9; const int INF = 0x7fffffff; /************************************END DEFINE*********************************************/ struct Side{ int u,v,cap,w; int nxt; }side[maxn*6]; int head[maxm],pre[maxm],dis[maxm]; bool vis[maxm]; int min_cost; int cnt; void init(){ cnt = 0; clr(head,-1); } void add_side(int u,int v,int cap,int w){ side[cnt].u = u; side[cnt].v = v; side[cnt].cap = cap; side[cnt].w = w; side[cnt].nxt = head[u]; head[u] = cnt++; side[cnt].u = v; side[cnt].v = u; side[cnt].cap = 0; side[cnt].w = -w; side[cnt].nxt = head[v]; head[v] = cnt++; } int spfa(int s,int t){ queue<int>q; while(!q.empty())q.pop(); FOR(i,s,t){ vis[i] = 0; dis[i] = INF; pre[i] = -1; } dis[s] = 0; q.push(s); vis[s] = 1; while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i=head[u];i!=-1;i=side[i].nxt){ int v = side[i].v; if(side[i].cap && dis[u]+side[i].w < dis[v]){ dis[v] = dis[u]+side[i].w; pre[v] = i; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } if(pre[t] == -1)return 0; return 1; } void End(int t){ int mid = INF; int k = pre[t]; while(k!=-1){ mid = min(mid,side[k].cap); k = pre[side[k].u]; } k = pre[t]; while(k!=-1){ side[k].cap-=mid; side[k^1].cap+=mid; k=pre[side[k].u]; } min_cost += dis[t]*mid; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF){ init(); while(m--){ int u,v,w; RIII(u,v,w); add_side(u,v,1,w); add_side(v,u,1,w); } add_side(0,1,2,0); add_side(n,n+1,2,0); int s = 0,t = n+1; min_cost = 0; while(spfa(s,t))End(t); printf("%d\n",min_cost); } return 0; }
[/code]
相关文章推荐
- R语言-差分要注意的问题
- 图表控件TeeChart干货分享(绘制2D、3D实时曲线---VC++示例源代码--网络首发)
- 关于actionBar 返回按钮
- POJ_2446_Chessboard
- [LeetCode] Palindrome Partitioning II
- 如何搭建Mantis 缺陷管理系统
- IOS RSA加密解密
- HDU 5411 CRB and puzzle (Dp + 矩阵快速幂)
- 竞品分析脑图
- hibernate简单入门知识
- 多线程GCD
- STL学习----入门(1)[memory]
- oracle学习笔记一:用户管理(3)用户口令管理
- linux下将中文文件名文件cp到windows目录下后文件名乱码问题的解决
- HDU 4135-Co-prime(容斥求区间内与N互质的个数(队列||位运算))
- Palindrome Number
- linux c/c++ 后台开发基础之:c++日志模块
- The Dole Queue(UVA 133)
- ZOJ 3435 Ideal Puzzle Bobble (莫比乌斯反演基础题)
- 范式的数据库具体解释