E - Easy Dijkstra Problem(求最短路)
Description
Determine the shortest path between the specified vertices in the graph given in the input data.
Hint: You can use Dijkstra's algorithm.
Hint 2: if you're a lazy C++ programmer, you can use set and cin/cout (with sync_with_stdio(0)) - it should suffice.
Input
first line - one integer - number of test cases For each test case the numbers V, K (number of vertices, number of edges) are given,Then K lines follow, each containing the following numbers separated by a single space:ai, bi, ci ,It means that the graph being described contains an edge from ai to bi,with a weight of ci.Below the graph description a line containing a pair of integers A, B is present.The goal is to find the shortest path from vertex A to vertex B.All numbers in the input data are integers in the range 0..10000.
Output
For each test case your program should output (in a separate line) a single number C - the length of the shortest path from vertex A to vertex B. In case there is no such path, your program should output a single word "NO" (without quotes)
Example
Input:
3
3 2
1 2 5
2 3 7
1 3
3 3
1 2 4
1 3 7
2 3 1
1 3
3 1
1 2 4
1 3 Output:
12
5
NO
解题思路:坑题,WA了好几发=_=||原来题目说明的是顶点a到顶点b是一条有向边,即a-->b,而不是无向图求单源最短路,裸题(邻接矩阵)水过!
AC代码:
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int t,n,k,a,b,c,st,ed,dis[maxn],cost[maxn][maxn];bool flag,vis[maxn];
void dijkstra(){
for(int i=;i<=n;++i)
dis[i]=cost[st][i];
dis[st]=;vis[st]=true;
for(int i=;i<n;++i){
int k=-;
for(int j=;j<=n;++j)
if(!vis[j]&&(k==-||dis[k]>dis[j]))k=j;
if(dis[k]==INF){flag=true;break;}//如果此时没有最小值即为INF,说明肯定是达不到终点ed,直接退出循环
if(k==-)break;
vis[k]=true;
for(int j=;j<=n;++j)
if(!vis[j])dis[j]=min(dis[j],dis[k]+cost[k][j]);
}
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
cost[i][j]=cost[j][i]=(i==j?:INF);
memset(vis,false,sizeof(vis));
while(k--){
scanf("%d%d%d",&a,&b,&c);
cost[a][b]=min(cost[a][b],c);//去重
}
scanf("%d%d",&st,&ed);
flag=false;
dijkstra();
if(flag)printf("NO\n");
else printf("%d\n",dis[ed]);
}
return ;
}
E - Easy Dijkstra Problem(求最短路)的更多相关文章
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 2019中山纪念中学夏令营-Day14 图论初步【dijkstra算法求最短路】
Dijkstra是我学会的第一个最短路算法,为什么不先去学SPFA呢?因为我在luogu上翻到了一张比较神奇的图: 关于SPFA -它死了 以及网上还有各位大佬的经验告诉我:SPFA这玩意很容易被卡. ...
- Dijkstra算法求最短路模板
Dijkstra算法适合求不包含负权路的最短路径,通过点增广.在稠密图中使用优化过的版本速度非常可观.本篇不介绍算法原理.只给出模板,这里给出三种模板,其中最实用的是加上了堆优化的版本 算法原理 or ...
- dijkstra算法求最短路
艾兹格·W·迪科斯彻 (Edsger Wybe Dijkstra,1930年5月11日~2002年8月6日)荷兰人. 计算机科学家,毕业就职于荷兰Leiden大学,早年钻研物理及数学,而后转为计算学. ...
- 关于dijkstra求最短路(模板)
嗯.... dijkstra是求最短路的一种算法(废话,思维含量较低, 并且时间复杂度较为稳定,为O(n^2), 但是注意:!!!! 不能处理边权为负的情况(但SPFA可以 ...
- Aizu-2249 Road Construction(dijkstra求最短路)
Aizu - 2249 题意:国王本来有一个铺路计划,后来发现太贵了,决定删除计划中的某些边,但是有2个原则,1:所有的城市必须能达到. 2:城市与首都(1号城市)之间的最小距离不能变大. 并且在这2 ...
- ACM - 最短路 - AcWing 849 Dijkstra求最短路 I
AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- BZOJ_1001_狼抓兔子_(平面图求最小割+对偶图求最短路)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec ...
随机推荐
- 【springmvc】传值的几种方式&&postman接口测试
最近在用postman测试postman接口,对于springmvc传值这一块,测试了几种常用方式,总结一下.对于postman这个工具的使用也增加了了解.postman测试很棒,有了工具,测试接口, ...
- Codeforces 651C Watchmen【模拟】
题意: 求欧几里得距离与曼哈顿距离相等的组数. 分析: 化简后得到xi=xj||yi=yj,即为求x相等 + y相等 - x与y均相等. 代码: #include<iostream> #i ...
- AtCoder Grand Contest 011 E - Increasing Numbers(灵性乱搞)
题意: 当一个整数高位数字总不小于低位数字,或者说写成字符串之后单调不下降,称之为上升数.求一个整数最少能表示为多少个上升数的和.(n<=1e500000) 分析: 考虑那些不下降的数字,一定可 ...
- 选择器的使用(empty选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- mkswap,swapon, swapoff命令:创建交换分区
linux支持虚拟内存,用作虚拟内存的硬盘部分被称为交互空间(swap space),虚拟内存是指使用磁盘当作内存的扩展,这样可用内存的大小就相应的增大了.内核会将暂时不用的内存块的内容写到硬盘上,从 ...
- C# .NET Visual Studio VS2008如何显示行号
工具-选项,然后勾选"显示所有设置",然后在文本编辑器下面找到所有语言,勾选"行号"即可.
- 使用Scroller制作滑块开关ToggleButton
Scroller这个类在自己定义view中使用的还算是非常频繁的,和它名字一样.我们通常是在控制滑动的时候使用Scroller,以便让view滑动起来不那么生硬.在官方的解释上,Scroller是一个 ...
- Nginx中配置vue,react项目地址
如题 像以前在Nginx中配置域名解析的时候只需要在conf.d文件夹下添加对应的xx.conf文件(当然了你也可以在nginx.conf)下配置. 如果是以前的老项目只需要在配置文件中server内 ...
- leetcode第一刷_Best Time to Buy and Sell Stock
这样的题就不要去考虑N^2的算法了.肯定会超时的.乍一看,非常可能会想到贪心,可是普通的贪心思路是不行的,比方想找到一个最小值用来买入.尽管它跟最大值之间的差一定是最好的,可是最大值出如今它前面就不行 ...
- 第一个GraphX程序
程序功能:收集顶点指向的邻居中所在地 /* * 找出每一个顶点所指向的邻居中所在的地区 */ import org.apache.spark.SparkContext import org.apach ...