POJ2135 Farm Tour 最小费用流
来源:互联网 发布:在家兼职淘宝客服58 编辑:程序博客网 时间:2024/05/24 01:07
Farm TourTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15844 Accepted: 6135DescriptionWhen 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.OutputA single line containing the length of the shortest tour.Sample Input4 51 2 12 3 13 4 11 3 22 4 2Sample Output6SourceUSACO 2003 February Green
题意:
无向图 ,从1走到n,再从n走回1 ,每一条边不能重复走,求最短距离
显然 求2遍最短路不能保证正确性
如果将边的距离看做费用,每条边容量为1,表示只能走一遍
源点S到1的容量为2,费用为0
n到汇点T容量为2,费用为0
那就转化为了最小费用流问题
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int N = 1e3 + 5;const int M = 5e4;struct Edge{ int to,c,w,next;}edge[M];int head[N];int nume=0;inline void addEdge(int k,int u,int v,int c,int w){ edge[k].to=v, edge[k].c=c, edge[k].w=w, edge[k].next=head[u]; head[u]=k;}inline void addEdge(int u,int v,int c,int w){ addEdge(nume++,u,v,c,w); addEdge(nume++,v,u,0,-w);}void init(int n){ fill(head,head+n+1,-1); nume=0;}bool used[N];int dis[N],load[N],p[N];//距离 ,前驱边,前驱点bool spfa(int s,int e,int n){ deque<int>que; for(int i=0;i<=n;++i){ dis[i]=INF; load[i]=p[i]=-1; used[i]=false; } que.push_back(s); dis[s]=0; used[s]=true; while(!que.empty()){ int u=que.front(); que.pop_front(); used[u]=false; for(int i=head[u];i!=-1;i=edge[i].next){ if(edge[i].c>0){ int v=edge[i].to; if(dis[v]>dis[u]+edge[i].w){ dis[v]=dis[u]+edge[i].w; p[v]=u; load[v]=i; if(used[v]==false){ used[v]=true; que.push_back(v); } } } } } return dis[e]!=INF;}int min_cost_flow(int s,int t,int n){ int ansflow=0,anscost=0; while(spfa(s,t,n)){ int u=t; int f=INF; while(p[u]!=-1){ f=min(f,edge[load[u]].c); u=p[u]; } u=t; while(p[u]!=-1){ edge[load[u]].c-=f; edge[load[u]^1].c+=f; u=p[u]; } anscost+=dis[t]*f; ansflow+=f; } return anscost;}void slove(int n,int m){ int s=n+1,t=n+2; init(n+2); for(int i=1;i<=m;++i){ int u,v,w; 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",min_cost_flow(s,t,n+2));}int main(){ //freopen("/home/lu/Documents/r.txt","r",stdin); //freopen("/home/lu/Documents/w.txt","w",stdout); int n,m; while(~scanf("%d%d",&n,&m)){ slove(n,m); } return 0;}
0 0
- 【POJ2135】Farm Tour 最小费用最大流
- Poj2135 Farm Tour (最小费用流)
- Poj2135 Farm Tour 最小费用流
- poj2135 Farm Tour 最小费用最大流
- poj2135 Farm Tour(最小费用流)
- POJ2135 Farm Tour 【最小费用最大流】
- POJ2135 Farm Tour 最小费用流
- poj2135 Farm Tour -最小费用流
- POJ2135 Farm Tour 最小费用流
- POJ2135 Farm Tour(最小费用流)
- POJ2135 Farm Tour(最小费用最大流)
- POJ2135--Farm Tour(最小费用最大流)
- 【POJ2135】Farm Tour(最小费用最大流)
- POJ2135 Farm Tour 无向图最小费用流
- POJ2135-Farm Tour(固定流量的最小费用流)
- POJ2135 Farm Tour —— 最小费用最大流
- POJ2135 Farm Tour(最小费用最大流裸题)
- poj2135-费用流&费用流模板-Farm Tour
- 解决Kali Linux没有声音
- 农历操作工具类
- NavigationView的用法
- 字符串操作工具类
- 数据库事务的四大特性以及事务的隔离级别
- POJ2135 Farm Tour 最小费用流
- Stanford机器学习-Linear Regressioon with One Variable(1)
- 洛谷 P1605 迷宫
- 手机号码验证工具类
- 深入浅出Lucene Analyzer
- LightOJ 1149 Factors and Multiples【最小点覆盖】(这些图论专题的题目都好裸啊)
- Java编程思想重点笔记
- 运行python 时出现TypeError: obj() takes no parametrs
- spring中bean的scope属性