[原]poj-2680-Choose the best route-dijkstra(基础最短路)
题目大意: 已知n 个点,m条路线,s为终点;给出m条路线及其权值;给出w个起点,求最短路!
思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可。
AC代码如下:
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- using namespace std;
- #define INF 1000000
- #define M 1010
- int n, m, s;
- int d[M];
- int w[M][M];
- int v[M];
- void dijkstra(int s)
- {
- memset(v,0,sizeof(v));
- memset(d,INF,sizeof(d));
- d[s] = 0;
- for(int i = 0; i < n; i++)
- {
- int x, m = INF; //WA了半天找不到的bug,就是这里;开始忘了放在循环里了,ORZ。。。所以m会一直是0.。不WA才怪。
- for(int j = 1; j <= n; j++)
- if(!v[j]&&d[j] <= m) m = d[x = j];
- v[x] = 1;
- for(int j = 1; j <= n; j++)
- d[j] = d[j] < d[x] + w[x][j]? d[j]:d[x] + w[x][j];
- }
- }
- void init(int n)
- {
- for(int i = 1; i <= n; i++)
- for(int j = 1; j <= n; j++)
- w[i][j] = INF;
- }
- int main()
- {
- while(scanf("%d%d%d",&n, &m, &s) == 3)
- {
- init(n);
- for(int i = 0; i < m; i++)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- if(w[b][a] > c)
- w[b][a] = c;
- }
- dijkstra(s);
- int la,st,Min = INF;
- scanf("%d",&la);
- for(int i = 0; i < la; i++)
- {
- scanf("%d",&st);
- if(d[st] < Min) Min = d[st];
- //cout<<st<<"*"<<d[st]<<endl;
- }
- if(Min < INF)
- printf("%d\n",Min);
- else
- cout<<"-1"<<endl;
- }
- return 0;
- }
[原]poj-2680-Choose the best route-dijkstra(基础最短路)的更多相关文章
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- 最短路问题-- Dijkstra Choose the best route
Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...
随机推荐
- bzoj 3232 01分数规划+最大权封闭子图判定
我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...
- poj 1300 Door Man 欧拉回路
题目链接:http://poj.org/problem?id=1300 You are a butler in a large mansion. This mansion has so many ro ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...
- FormCreate & FormActivate & FormShow执行顺序演示
procedure TForm1.FormCreate(Sender: TObject);begin form1.Caption:=form1.Caption +'+Create'; end; pr ...
- Codeforces Round #FF (Div. 2)
又一场中国场,果然注定被虐的场... A,B:都很水,差不多模拟就好了: C题:CF难得的题意简洁, 我们可以预处理出从左到右递增的数列个数, 举个例子:1 3 2 4 5 7 L[I] ...
- 在JavaScript中实现yield,实用简洁实现方式。
原题还是老赵的: http://blog.zhaojie.me/2010/06/code-for-fun-iterator-generator-yield-in-javascript.html 原以为 ...
- javascript中的JSON序列化与反序列化
简单粗暴上代码: function create() { this.name = "jack"; this.sex = "man"; } create.prot ...
- Android内存泄漏问题(一)
前言 不少人认为JAVA程序,因为有垃圾回收机制,应该没有内存泄露. 其实如果我们一个程序中,已经不再使用某个对象,但是因为仍然有引用指向它,垃圾回收器就无法回收它,当然该对象占用的内存就无法被使用, ...
- typedef (还需经常看看加深理解)
看了 c++primer 1,typedef名字 typedef定义以关键字typedef开始,后面是 数据类型+标示符. 并未引入新的类型,只是现有数据类型的同义词 例: typedef doubl ...
- 华为上机:求2的N次幂的值
求2的N次幂的值 描述: 求2的N次幂的值(N最大不超过31,用位运算计算,结果以十六进制进行显示). 运行时间限制: 无限制 内存限制: 无限制 输入: 数字N 输出: 2的N次方(16进制,需要按 ...