POJ2135
来源:互联网 发布:linux 从u盘导入 编辑:程序博客网 时间:2024/05/28 23:21
Farm Tour
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13644 Accepted: 5169
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
【题意】:农夫要带着客人去参观自己的农场的谷仓,首先从家到谷仓,然后从谷仓回到家中,要求两次走的路径不同,而且距离之和最小
【思路】:纯粹的最小费用最大流问题,而这道题也可以锻炼最短路的能力;不多说,首先设置一个超级源点0,一个超级汇点N+1,因为是1到N的双源最短路,所以直接将0与1相连,cap为2,N与N+1相连,cap为2,因为是无向图,所以每条边要连两次。
【给一组测试数据】:
10 157 1 137846 1 316924 9 163185 10 52110 3 164205 2 118176 4 290708 5 136142 9 171688 1 192601 2 60762 3 10383 6 129172 6 1781510 4 26493输出:56929
【代码】:
#include <iostream>#include <stdio.h>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#define INF 0xffff777#define ll long longusing namespace std;int n,m;struct edge{ int y,c,l,r;};vector<edge> g[10005];int dis[10005];int prev[10005],pree[10005];void add(int x,int y,int l){ g[x].push_back((edge){y,1,l,g[y].size()}); g[y].push_back((edge){x,0,-l,g[x].size()-1});}ll miflow(int s,int t){ ll res=0; while(1){ fill(dis,dis+n+2,INF); dis[s]=0; bool update=1; while(update){ update =0; for(int v=s;v<t;++v){ //printf(" dis 4= %d\n",dis[4]); if(dis[v]==INF) continue; for(int i=0;i<g[v].size();++i){ edge& e=g[v][i]; //printf("v= %d %d\n",v,i); if(e.c>0&&dis[e.y]>dis[v]+e.l){ //printf("v= %d %d e,y=%d\n",v,i,e.y); dis[e.y]=dis[v]+e.l; prev[e.y]=v; pree[e.y]=i; update=1; } } } } //printf("%d %d\n",dis[5],g[4][g[4].size()-1].c); if(dis[t]==INF) break; int a=INF; for(int v=t; v!=s; v=prev[v]) if( g[prev[v]][pree[v]].c< a ) a = g[prev[v]][pree[v] ].c; for(int v=t; v!=s; v=prev[v]) { //printf("%d %d\n",2,g[2][4].l); edge& e=g[prev[v]][pree[v]]; g[v][e.r].c += a; e.c -= a; } res += dis[t]*a; } return res;}int main(){ scanf("%d%d",&n,&m); int x,y,l; for(int i=1;i<=m;++i){ scanf("%d%d%d",&x,&y,&l); add(x,y,l); add(y,x,l); } g[0].push_back((edge){1,2,0,g[1].size()}); g[1].push_back((edge){0,0,0,g[0].size()-1}); g[n].push_back((edge){n+1,2,0,g[n+1].size()}); g[n+1].push_back((edge){n,0,0,g[n].size()-1}); printf("%lld\n",miflow(0,n+1)); return 0;}
1 0
- poj2135
- POJ2135
- poj2135
- POJ2135
- poj2135
- POJ2135
- poj2135
- poj2135 - Farm Tour
- poj2135 Farm Tour
- poj2135--Farm Tour
- poj2135 Farm Tour
- poj2135(模板题)
- POJ2135 Farm Tour (mcmf)
- POJ2135-Farm Tour
- 【poj2135】Farm Tour
- POJ2135 Farm Tour
- POJ2135: Farm Tour 题解
- POJ2135 Farm Tour
- Jetty的安装和配置
- 安卓开发中的工作笔记
- MyEclipse逆向生成实体类和Hibernate映射文件
- 高通msm8926 camera调试笔记
- Android中仿淘宝商品详情ViewPager页面数据手动滑动
- POJ2135
- Android Studio中常用设置与快捷键(私人珍藏,Eclipse转AS必看)
- (scissor , rock , paper) 三星题 剪刀石头布
- 苹果 nyoj 289
- Java多线程基础:进程和线程之由来
- Linux(RedHat 5.8)下 安装Jetty 部署 使用
- 【解决】Mysql字符集问题
- Linux 生成 core 文件的方法
- JavaScript高级程序设计之BOM之navigator 对象 第8.3讲笔记