POJ2135_Farm Tour(网络流/费用流)
来源:互联网 发布:finaldata软件 编辑:程序博客网 时间:2024/06/15 18:37
解题报告
题目传送门
题意:
一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路。
思路:
如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了。
一次mcmf模版。
#include <iostream>#include <cstring>#include <queue>#include <cstdio>#define inf 0x3f3f3f3fusing namespace std;int head[2010],dis[2010],vis[2010],pre[2010],f[2010],cnt,flow,cost,s,t,n,m;struct node { int v,cap,cost,next;} edge[50000];void add(int u,int v,int cost,int cap) { edge[cnt].v=v,edge[cnt].cost=cost,edge[cnt].cap=cap; edge[cnt].next=head[u],head[u]=cnt++; edge[cnt].v=u,edge[cnt].cost=-cost,edge[cnt].cap=0; edge[cnt].next=head[v],head[v]=cnt++;}int _spfa() { queue<int>Q; Q.push(s); for(int i=1; i<=n; i++)dis[i]=inf,pre[i]=vis[i]=f[i]=0; dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf; while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) { pre[v]=i; f[v]=min(edge[i].cap,f[u]); dis[v]=dis[u]+edge[i].cost; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } if(dis[t]==inf)return 0; flow+=f[t]; cost+=f[t]*dis[t]; if(flow==2)return 0; for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) { edge[i].cap-=f[t]; edge[i^1].cap+=f[t]; } return 1;}void mcmf() { cost=flow=0; while(_spfa()); printf("%d\n",cost);}int main() { int u,v,w; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); s=1; t=n; while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w,1); add(v,u,w,1); } mcmf(); return 0;}
Farm Tour
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11402 Accepted: 4230
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.
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.
* 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 Output
6
2 0
- POJ2135_Farm Tour(网络流/费用流)
- HDU 3488Tour(网络流之最小费用流)
- POJ 2135 Farm Tour(网络流之费用流)
- 【HDU3488】【网络流】【最小费用最大流】Tour 题解
- HDU 3488 Tour 费用流
- 费用流 hdu1853 Cyclic Tour
- POJ_2135_Farm Tour(最小费用流)
- [费用流] HDU1853 Cyclic Tour
- HDU 1853 Cyclic Tour && HDU 3488Tour 最小费用流
- POJ 2135 Farm Tour(最小费用流)
- HDU 1853 Cyclic Tour //费用流
- 【POJ2135】Farm Tour 最小费用最大流
- Poj2135 Farm Tour (最小费用流)
- POJ 2135 Farm Tour(最小费用流)
- Poj2135 Farm Tour 最小费用流
- POJ 2135 Farm Tour (费用流)
- POJ 2135 Farm Tour 最小费用流
- poj 2135 Farm Tour 费用流
- zoj1456 Minimum Transport Cost
- NFS服务更新exportfs后出现问题,但不影响使用
- 汉诺塔问题
- iBatis select
- hosts
- POJ2135_Farm Tour(网络流/费用流)
- 各种加解密算法分类及其各自的安全性能对比
- A:Simple prefix compression来自百炼2
- nyoj 石子归并(1)
- cocos-2d动作
- 嵌入式C语言面试题(经典)
- POJ 2778DNA Sequence AC自动机 + 矩阵快速幂
- SQL语句集锦
- NFS挂载根文件系统出现的错误(利用NFS服务挂载NFS根文件系统 .)