Adventurous Driving - POJ 2679 最短路+负环
来源:互联网 发布:比邻交友软件 编辑:程序博客网 时间:2024/06/06 02:52
Adventurous Driving
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1511 Accepted: 426
Description
After a period of intensive development of the transportation infrastructure, the government of Ruritania decides to take firm steps to strengthen citizens' confidence in the national road network and sets up a compensation scheme for adventurous driving (CSAD). Those driving on a road with holes, bumps and other entertaining obstacles get compensation; those driving on a decent road pay tax. These compensations and taxes are obtained and paid in cash on entry on each road and depend on the entry point on the road. What you get and pay driving on a road from A to B may be different from what you get and pay driving on the same road from B to A. The Ruritarian authorities call fee the amount of money paid as tax or obtained as compensation on entry on a road. A positive fee is a tax; a negative fee stands for compensation.
John Doe plans to take advantage of CSAD for saving money he needs to repair his old car. When driving from A to B, John follows a path he calls optimal: a path that is rewarding and has the minimal length out of the paths with the minimal weight from A to B. In John's opinion, a path is rewarding if all the roads in the path are rewarding, and a road (X,Y) is rewarding if it has the minimal entry fee out of the roads leaving X. The weight of a path is the sum of the entry fees paid along the path. The length of a path cumulates the length of the roads in the path. The problem is helping John to compute the weight and the length of an optimal path from A to B on a given map.
For example, on the illustrated road map vertices designate cities and edges stand for roads. The label fuv[L]fvu of the road (u,v) shows the fee fuv for driving from u to v, the fee fvu for driving from v to u, and the length L of the road. The path (0,2,4,3,5) from 0 to 5 is optimal: it is rewarding, has weight 2 (-1+3+0+0) and length 50 (5+10+5+30). The path (0,1,4,3,5), although rewarding and of weight 2, has length 51. The path (0,3,5) has weight 0 and length 20 but it is not rewarding.
John Doe plans to take advantage of CSAD for saving money he needs to repair his old car. When driving from A to B, John follows a path he calls optimal: a path that is rewarding and has the minimal length out of the paths with the minimal weight from A to B. In John's opinion, a path is rewarding if all the roads in the path are rewarding, and a road (X,Y) is rewarding if it has the minimal entry fee out of the roads leaving X. The weight of a path is the sum of the entry fees paid along the path. The length of a path cumulates the length of the roads in the path. The problem is helping John to compute the weight and the length of an optimal path from A to B on a given map.
For example, on the illustrated road map vertices designate cities and edges stand for roads. The label fuv[L]fvu of the road (u,v) shows the fee fuv for driving from u to v, the fee fvu for driving from v to u, and the length L of the road. The path (0,2,4,3,5) from 0 to 5 is optimal: it is rewarding, has weight 2 (-1+3+0+0) and length 50 (5+10+5+30). The path (0,1,4,3,5), although rewarding and of weight 2, has length 51. The path (0,3,5) has weight 0 and length 20 but it is not rewarding.
Input
Write a program that reads several data sets from a text file. Each data set encodes a road map and starts with four integers: the number 1<=n<=1100 of towns on the map, the number 0<=m<=5000 of roads, the departure town 0<=A<=n-1, and the destination town 0<=B<=n-1. Follow m data quintuples (u,v,fuv[L]fvu), where u and v are town identifiers (integers in the range 0..n-1), 100<=fuv, fvu<=100 are integer fees for driving on the road (u,v), and 1<=L<=100 is the integer length of the road. The quintuples may occur in any order. Except the quintuples, which do not contain white spaces, white spaces may occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set, the program prints – from the beginning of a line – the weight and the length of an optimal path, according to John's oppinion, from A to B. If there is no optimal path from A to B the text VOID is printed. If the weight of the optimal path from A to B has no lower bound the text UNBOUND is printed.
Sample Input
3 3 0 2 (0,1,0[1]0) (0,2,1[1]0) (1,2,1[1]0)3 3 0 2 (0,1,-1[1]1) (0,2,0[1]0) (1,2,0[1]1)7 11 0 5 (0,1,-1[6]4) (0,2,-1[5]4) (0,3,0[1]0) (1,4,3[10]1)(2,4,3[10]1) (3,4,0[5]0) (3,5,0[30]0) (3,5,1[20]0) (4,6,0[3]1) (6,5,1[8]0) (6,6,0[2]-1)
Sample Output
VOIDUNBOUND2 50
题意:每次给定u,v,fuv[len],fvu,len表示uv之间的,fuv表示从u到v的费用,fvu表示从v到u的费用。每次点只能走它下一个费用最低的路线。求从A到B在花费最小的情况下,路径长度的最小值,并输出这两个值。如果不存在路径输出VOID,如果有负环输出UNBOUND。
思路:首先保证每次点只走下一个费用最低的路线。然后用spfa,求是否存在负环。如果有负环,判断这些负环内的点是否能通向B,如果可以的话,就是UNBOUND,否则输出结果。
AC代码如下:
#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{ int v,len,cost,next;}edge[10010];int n,m,A,B,Head[1110],tot,INF=1e9,c[1110],length[1110],times[1110];queue<int> qu;bool vis[1110];char ch;void add(int u,int v,int len,int cost){ if(Head[u]!=-1 && edge[Head[u]].cost<cost) return; if(Head[u]!=-1 && edge[Head[u]].cost>cost) Head[u]=-1; edge[tot].v=v; edge[tot].len=len; edge[tot].cost=cost; edge[tot].next=Head[u]; Head[u]=tot++;}void spfa(int u){ int i,j,k,v; for(i=0;i<n;i++) c[i]=length[i]=INF; memset(times,0,sizeof(times)); memset(vis,0,sizeof(vis)); while(!qu.empty()) qu.pop(); c[u]=length[u]=0; qu.push(u);vis[u]=1; while(!qu.empty()) { u=qu.front(); qu.pop(); vis[u]=0; if(++times[u]>n+5) return; for(i=Head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(c[v]>c[u]+edge[i].cost || (c[v]==c[u]+edge[i].cost && length[v]>length[u]+edge[i].len)) { c[v]=c[u]+edge[i].cost; length[v]=length[u]+edge[i].len; if(!vis[v]) { vis[v]=1; qu.push(v); } } } }}void dfs(int u){ int i,j,k,v; vis[u]=1; for(i=Head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(!vis[v]) dfs(v); }}int main(){ int i,j,k,u,v,fuv,fvu,len; while(~scanf("%d%d%d%d",&n,&m,&A,&B)) { memset(Head,-1,sizeof(Head)); tot=0; for(i=1;i<=m;i++) { while(scanf("%c", &ch), ch != '('); scanf("%d,%d,%d[%d]%d)", &u, &v, &fuv, &len, &fvu); add(u,v,len,fuv); add(v,u,len,fvu); } spfa(A); memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) if(times[i]>n) dfs(i); if(c[B]==INF) printf("VOID\n"); else if(vis[B]) printf("UNBOUND\n"); else printf("%d %d\n",c[B],length[B]); }}
0 0
- Adventurous Driving - POJ 2679 最短路+负环
- POJ 2679 Adventurous Driving
- zoj 2583 || poj 2679 Adventurous Driving
- 2679 Adventurous Driving 解题报告
- POJ-2679(最短路+负环判断)
- poj 3259 最短路负环
- poj 3259 -- Wormholes(最短路,负环)
- POJ 3259 Wormholes (最短路 SPFA 判断负环)
- poj1860(最短路,负环)
- poj3259Wormholes【最短路SPFA判断负环】
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
- poj 3259 Wormholes(最短路+spfa+判负回路)
- poj 3259 bellman最短路判断有无负权回路
- 最短路 SPFA 判断负环 静态邻接表(链式前向星) HDU 2544 最短路 POJ 3259 Wormholes
- 最短路之 SPFA(判环+负权)
- Wormholes 3259 (最短路+判断负环是否存在)
- poj3259Wormholes(bellman判断负环的问题最短路)
- POJ3259---Wormholes(最短路:验证存在负环)
- 可以使用框架但千万不要依赖框架
- 《Java编程思想》学习笔记3——内部类
- #ifdef 如何实现“与或”
- adobe ae cs6中文版汉化(11.02)win版汉化安装教程
- 配置虚拟机支持USB设备的热插拔
- Adventurous Driving - POJ 2679 最短路+负环
- Linux文件的默认权限与查找命令详解
- 取千位,百位,十位,个位
- python socket编程小解
- 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程
- python copy & deepcopy 区别
- 《Java编程思想》学习笔记4——集合容器
- 关于 js 算术运算时精度问题
- python第三方库推荐之tablib(用于导出数据到不同的文件格式)