POJ1724--ROADS
来源:互联网 发布:开关贴淘宝 编辑:程序博客网 时间:2024/05/21 22:57
Description
N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city Nthat he can afford with the amount of money he has.
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city Nthat he can afford with the amount of money he has.
Input
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
Notice that different roads may have the same source and destination cities.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
- S is the source city, 1 <= S <= N
- D is the destination city, 1 <= D <= N
- L is the road length, 1 <= L <= 100
- T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
Output
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2
Sample Output
11
/*城市100个*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 1008#define edge 11080#define inf 0x3f3f3f3fint vv[edge],len[edge],cost[edge],nxt[edge];int first[maxn];bool vis[maxn];int e,k,n,r,ans;inline int min(int a,int b){return a>b?b:a;}void addEdge(int u,int v,int l,int w){vv[e] = v;len[e] = l;cost[e] = w;nxt[e] = first[u];first[u] = e++;}void dfs(int x,int l,int c){if(l > ans) return;if(x == n) ans = l;for(int i = first[x];i != -1;i = nxt[i]){int v = vv[i];if(!vis[v] && c + cost[i] <= k){vis[v] = 1;dfs(v,l + len[i],c + cost[i]);vis[v] = 0;}}}int main(){//freopen("in.txt","r",stdin);while(scanf("%d%d%d",&k,&n,&r)==3){memset(first,-1,sizeof(first));memset(vis,0,sizeof(vis));e = 0;ans = inf;for(int i=1;i<=r;i++){int u,v,l,t;scanf("%d%d%d%d",&u,&v,&l,&t);addEdge(u,v,l,t);}vis[1] = 1;dfs(1,0,0);if(ans == inf) printf("-1\n");else printf("%d\n",ans);}return 0;}
- poj1724 ROADS
- poj1724 - ROADS
- ROADS poj1724
- POJ1724--ROADS
- POJ1724 ROADS
- POJ1724-ROADS
- poj1724 ROADS
- poj1724 ROADS
- POJ1724 ROADS
- poj1724——ROADS
- poj1724 ROADS (spfa + A*)
- poj1724 ROADS 解题报告
- poj1724 roads(dfs)
- poj1724——ROADS
- POJ1724 ROADS最短路问题
- poj1724 openjudge1724 ROADS 解题记录
- 【POJ1724】ROADS 某邪恶最短路
- poj1724--ROADS(最短路变形)
- 笔试面试算法
- POJ 1637 Sightseeing tour (混合图欧拉回路)
- iOS: NSObject的方法classFallbacksForKeyedArchiver
- 关于Preference的一点理解
- 杭电--2077--汉诺塔IV--数学题
- POJ1724--ROADS
- socket编程
- XML文件格式语法及DTD
- hdu1158 Employment Planning
- C语言几种常见排序算法(一)
- Android基础知识学习——界面转换(完善)
- java易错知识点梳理
- C语言小程序(一)、判断三角型类型
- ios7 beta3 下载