poj1724

ROADS

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10804   Accepted: 3976

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 N that 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 :

  • 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.

Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

Sample Output

11

Source

CEOI 1998

 1 //邻接表存储结构+剪枝,优化时间复杂度
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int MAXWAY = 10010;
 8 const int MAXCITY = 110;
 9 const int INF = 0xffffff0;
10 struct Node{
11     int s,d,l,t;
12     int next;      //邻接表的表头指针
13 }Node[MAXWAY];
14 int k,n,r;
15 int totallen;   //存储当前路径中的路径长度
16 int totalcost; //存储当前路径中需要的话费
17 int minLen;    //存储当前情况下最短的路径长度
18 int visited[110]; //存储是否访问过某个城市
19
20 int head[MAXWAY];   //存储链表信息
21
22 void DFS(int i)
23 {
24     if(i==n)
25     {
26         minLen = min(minLen,totallen);
27         return ;
28     }
29     else
30     {
31         for(int j=head[i];j!=-1;j=Node[j].next)   //遍历 以i为起点的其他的所有
32         {
33             if(!visited[Node[j].d])
34             {
35                 if(totalcost+Node[j].t>k)  //如果加上当前的这条路的费用超过了k,则跳过
36                     continue;
37                 if(totallen+Node[j].l>minLen)  //如果在费用没有超过的情况下加上该条路的长度,超过了当前的最短长度,则跳过
38                     continue;
39                 totallen = totallen + Node[j].l;
40                 totalcost = totalcost + Node[j].t;
41                 visited[Node[j].d] = 1;
42                 DFS(Node[j].d);
43                 visited[Node[j].d] = 0;
44                 totallen -= Node[j].l;
45                 totalcost -= Node[j].t;
46
47             }
48         }
49     }
50 }
51
52 int main()
53 {
54     while(scanf("%d%d%d",&k,&n,&r)!=EOF)
55     {
56         memset(head,-1,sizeof(head));
57         memset(visited,0,sizeof(visited));
58         for(int i=0;i<r;i++)    //接受数据同时,利用头插法建立邻接表
59         {
60             scanf("%d%d%d%d",&Node[i].s,&Node[i].d,&Node[i].l,&Node[i].t);
61             Node[i].next = head[Node[i].s];      //如果当前不存在元素,则此时的Node[i]就是尾节点,它的next是-1
62             //否则,Node[i].next就是指向当前的链表最头部的那个Node元素。
63             head[Node[i].s] = i;   //修改当前的head[Node[i].s]的指向,使head[i]的值始终指向该条链表的头部元素,以便执行头插法
64         }
65         totallen = 0;
66         totalcost = 0;
67         minLen = INF;
68         DFS(1);
69         if(minLen<INF)
70             printf("%d\n",minLen);
71         else
72             printf("-1\n");
73     }
74     return 0;
75 }

poj1724

时间: 2024-10-31 08:35:07

poj1724的相关文章

poj1724 ROADS

题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N<=1000<=K<=100001<=R<=10000每条路的长度 L, 1 <= L <= 100每条路的过路费T , 0 <= T <= 100 思路: 用d[i][j]表示从源点走到城市i并且花费为j的时候经过的最短距离.若在后续的搜索中,再次走到i

poj-1724(bfs+优先队列)

题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n 解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是用一个优先队列,每次弹出路径最短的边,计算当前花费和下条边的花费如果小于k,那么这条边就可行. 很多博客写是迪杰斯特拉+heap,我觉得没有松弛过程的应该算不上单元最短路的写法把(QAQ); 代码: #include<iostream> #include<algorithm> #inc

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅

POJ题目分类

初期: 一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,he

嗷嗷嗷,kuangbin大大博客上拉的题

正在学(learning),未学(waiting),已学(cut  vovering) 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.  

转:转一个搞ACM需要的掌握的算法. .

要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红,  发挥自己的长处,这才是重要的. 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,  因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打  出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)

算法初学者指南

摘自网络,对于这个训练计划,我只能膜拜,~ 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15 分钟内打完,甚至关掉显示器都可以把程序打 出来. 1.最短路(Floyd.Dijstra,BellmanFord) 2. 最小生成树(先写个prim,kruscal要用并查集,不好写) 3.大数(高精度)加减乘除 4.二分查找. (代码可在五行以内) 5.叉乘.判线段相交.然后写个凸包. 6.BFS.DFS,同时熟练hash表(