http://poj.org/problem?id=2679

Adventurous Driving
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1596   Accepted: 455

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.

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

VOID
UNBOUND
2 50

Hint

An input/output sample is in the table above. The first data set encodes a road map with no optimal path from 0 to 2. The second data set corresponds to a map whose optimal path from 0 to 2 has an unbound weight. The third data set encodes the road map shown in the above figure.
 
题意:给出一个有向图,每条边有一个费用和长度。给出一个起点一个终点。要求从起点走到终点,每个点的出边必须走费用最小的或者并列最小的,如果按照要求不能走到终点,就输出VOID。然后如果走过的路费用可以无限小,那么就输出 BOUND,否则就计算从起点到终点的最小费用和最小长度,费用最小优先,同样小就长度最小优先。
 
思路:这题一开始看不懂题意,觉得貌似挺水的,看懂题意后一直在第三个样例跑成负环,后来从别人那里才知道这题是要从起点到终点的路径判断,而平时写的是对于整个图判断。首先把每个点最小费用的出边记录下来,把大于这个最小费用的出边删除,这样的图才符合条件。因为要判断从起点到终点的路径是否有负环,平时写的都是对于整个图的判断负环,所以要建立一个反向图,跑一下DFS,用一个vis数组记录从终点跑出去可以经过哪些点,如果不可以经过的话,一定是不会出现在路径上的,那么就删除这些点之后跑SPFA就可以判断负环了,如果不是负环就可以输出最小的 fee 和 dis。
 
这里学习到好多vector的东西。。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
#define N 1110
struct edge
{
int l, w, v;
edge () {}
edge (int v, int w, int l) : v(v), w(w), l(l) {}
};
int st, ed, lfee[N], vis[N], dis[N], fee[N], cnt[N], n, m;
vector <vector<edge> > G, R;
//就是vector<edge> G[N]; void add(vector<vector<edge> > &G, int u, int v, int w, int l)
{
G[u].push_back(edge(v, w, l));
} //删除不是最小费用的边
void edge_clear()
{
for(int i = ; i < n; i++) {
for(vector<edge>::iterator p = G[i].begin(); p != G[i].end(); ) {
if(p->w > lfee[i]) {
p = G[i].erase(p);
} else {
p++;
}
}
}
} //删除从起点到终点不会走过的点
void node_clear()
{
for(int i = ; i < n; i++) {
if(!vis[i]) {
G[i].clear();
continue;
}
for(vector<edge>::iterator p = G[i].begin(); p != G[i].end(); ) {
if(!vis[p->v]) {
p = G[i].erase(p);
} else {
p++;
}
}
}
} //将图翻转
void reg()
{
R = vector<vector<edge> > (n);
for(int i = ; i < n; i++) {
for(vector<edge>::iterator p = G[i].begin(); p != G[i].end(); p++) {
add(R, p->v, i, p->w, p->l);
}
}
} //标记从终点走出去可以经过哪些点
void dfs(int u)
{
vis[u] = ;
for(int i = ; i < R[u].size(); i++) {
int v = R[u][i].v;
if(!vis[v]) dfs(v);
}
} bool spfa()
{
for(int i = ; i <= n; i++) {
dis[i] = INF; fee[i] = INF;
}
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
dis[st] = ;
fee[st] = ;
vis[st] = ;
queue <int> que;
while(!que.empty()) que.pop();
que.push(st);
while(!que.empty()) {
int u = que.front(); que.pop();
cnt[u]++;
if(cnt[u] > n) return false;
vis[u] = ;
for(int i = ; i < G[u].size(); i++) {
int v = G[u][i].v, w = G[u][i].w, l = G[u][i].l;
if(fee[v] >= fee[u] + w) {
if(fee[v] > fee[u] + w) {
fee[v] = fee[u] + w;
dis[v] = dis[u] + l;
if(!vis[v]) {
vis[v] = ;
que.push(v);
}
} else if(dis[v] > dis[u] + l) {
dis[v] = dis[u] + l;
if(!vis[v]) {
vis[v] = ;
que.push(v);
}
}
}
}
}
} int main()
{
while(~scanf("%d%d%d%d", &n, &m, &st, &ed)) {
memset(lfee, INF, sizeof(lfee));
G.clear(); R.clear();
G = vector<vector<edge> > (n);
for(int i = ; i < m; i++) {
int u, v, uv, vu, l;
scanf(" (%d,%d,%d[%d]%d)", &u, &v, &uv, &l, &vu);
add(G, u, v, uv, l);
add(G, v, u, vu, l);
if(lfee[u] > uv) lfee[u] = uv;
if(lfee[v] > vu) lfee[v] = vu;
//记录出边的最小的费用
} memset(vis, , sizeof(vis));
edge_clear();
reg();
dfs(ed);
if(!vis[st]) {
printf("VOID\n");
continue;
}
node_clear();
bool flag = spfa();
if(!flag) printf("UNBOUND\n");
else printf("%d %d\n", fee[ed], dis[ed]);
}
return ;
}
 

POJ 2679:Adventurous Driving(SPFA+DFS)的更多相关文章

  1. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  2. 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)

    题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...

  3. 题目1008:最短路径问题(SPFA算法)

    问题来源 http://ac.jobdu.com/problem.php?pid=1008 问题描述 给定一个G(V,E)有向图,起点s以及终点t,求最短路径. 问题分析 典型的单源最短路径问题,可以 ...

  4. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  5. POJ 3318:Matrix Multiplication(随机算法)

    http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...

  6. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  7. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  8. 【题解】洛谷P2296 [NOIP2014TG] 寻找道路(SPFA+DFS)

    题目来源:洛谷P2296 思路 一开始看还以为是一道水题 虽然本来就挺水的 本道题的难点在于如何判断是否路径上的点都会直接或者间接连着终点 我们需要在一开始多建一个反向图 然后从终点DFS回去 把路径 ...

  9. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

随机推荐

  1. Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8)

    delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__std ...

  2. python3. 报“ImportError: No module named ‘MySQLdb'”

    需要安装PyMysql pip install PyMysql 然后在程序中引入 import pymysql pymysql.install_as_MySQLdb() app.config['SQL ...

  3. XAML的命名空间

    原文:XAML的命名空间 一个最简单的XAML例子   <Window x:Class="WpfApplication1.MainWindow" xmlns="ht ...

  4. 高手问答精选:Go 语言 —— 云计算时代的 C 语言(类似于一个FAQ)

    Go 语言被称为云计算时代的 C 语言,它在软件开发效率和运行效率之间做出了绝佳的权衡.这使得它既适应于互联网应用的极速开发,又能在高并发.高性能的开发场景中如鱼得水.正因如此,许多互联网公司,尤其是 ...

  5. mfc动态显示图片

    参考:https://blog.csdn.net/pudongdong/article/details/69396600 之前写过win32动态显示图片的博客,这次用到了mfc.原理是一样的. OnI ...

  6. Delphi调用爷爷类的方法(重新构造TMethod的data和code部分,其中Code指向祖父类的方法)

    Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下: 假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方 ...

  7. MySql5.7.11 for Windows 安装(二)

    原文:MySql5.7.11 for Windows 安装(二) 安装之后,首先创建data文件夹(旧版本本来就有),管理员打开cmd,cd到bin文件夹,输入 mysqld –initialize- ...

  8. UWP -- Background Task 深入解析

    原文:UWP -- Background Task 深入解析 1. 重点 锁屏问题 从 Windows 10 开始,用户无须再将你的应用添加到锁屏界面,即可利用后台任务,通用 Windows 应用必须 ...

  9. 修改GitHub上项目语言显示

    问题 最近将自己以Scala为主语言写的博客放到github上了.由于使用了富文本编辑器.jQuery.Bootstrap等第三方插件,导致js.css等代码远远超过你自己写的代码. 于是也就成这样了 ...

  10. 制作Qt应用程序的插件(使用QtPlugin),对比DLL它是全平台通用的

    在Qt下,插件有两种形式,一种是用于QtCreator下,扩展IDE功能.另一种是用于扩展开发者的应用.本文要讲的是后者. 定义一个纯虚类作为插件接口 #include <QtPlugin> ...