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(求最短路)的更多相关文章

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

  2. 2019中山纪念中学夏令营-Day14 图论初步【dijkstra算法求最短路】

    Dijkstra是我学会的第一个最短路算法,为什么不先去学SPFA呢?因为我在luogu上翻到了一张比较神奇的图: 关于SPFA -它死了 以及网上还有各位大佬的经验告诉我:SPFA这玩意很容易被卡. ...

  3. Dijkstra算法求最短路模板

    Dijkstra算法适合求不包含负权路的最短路径,通过点增广.在稠密图中使用优化过的版本速度非常可观.本篇不介绍算法原理.只给出模板,这里给出三种模板,其中最实用的是加上了堆优化的版本 算法原理 or ...

  4. dijkstra算法求最短路

    艾兹格·W·迪科斯彻 (Edsger Wybe Dijkstra,1930年5月11日~2002年8月6日)荷兰人. 计算机科学家,毕业就职于荷兰Leiden大学,早年钻研物理及数学,而后转为计算学. ...

  5. 关于dijkstra求最短路(模板)

    嗯....   dijkstra是求最短路的一种算法(废话,思维含量较低,   并且时间复杂度较为稳定,为O(n^2),   但是注意:!!!!         不能处理边权为负的情况(但SPFA可以 ...

  6. Aizu-2249 Road Construction(dijkstra求最短路)

    Aizu - 2249 题意:国王本来有一个铺路计划,后来发现太贵了,决定删除计划中的某些边,但是有2个原则,1:所有的城市必须能达到. 2:城市与首都(1号城市)之间的最小距离不能变大. 并且在这2 ...

  7. ACM - 最短路 - AcWing 849 Dijkstra求最短路 I

    AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. BZOJ_1001_狼抓兔子_(平面图求最小割+对偶图求最短路)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec   ...

随机推荐

  1. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D

    D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. zoj2479 Cover the Rectangular Ground

    肯定是dfs搜一下的,但是呢存在一个很大的剪枝,也就是面积必定要是相等的,那么如何去操作呢,可以想到的是二进制枚举选取的方法,然后把方法中选取的矩形面积求和并判断一下即可,然后dfs搜索,要注意的是, ...

  3. Linux下搭建maven私服Nexus 3.2.1-01

    1. 私服介绍私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构建.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则, ...

  4. 解决confluence的乱码问题

    使用confluence时发现一些含有中文的页面中,中文都变成了问号. 继续搜索解决方案,发现时数据库中数据的格式不对, 在mysql中输入以下命令:   mysql> show variabl ...

  5. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  6. netty4与protocol buffer结合简易教程

    各项目之间通常使用二进制进行通讯,占用带宽小.处理速度快~ 感谢netty作者Trustin Lee.让netty天生支持protocol buffer. 本实例使用netty4+protobuf-2 ...

  7. openstack (1)----- NTP 时间同步服务

    一.标准时间 1.地球分为东西十二个区域,共计24个时区 2.格林威治作为全球标准时间即(GMT时间),东时区以格林威治时区进行加,而西时区则进行减 3.地球的轨道并非正圆,在加上自传速度逐年递减,因 ...

  8. web 开发之js---js 实现地址栏的表单提交加密编码

    以前在做嵌入式web时,曾经想过cgi加密,原来js可以直接实现,太好了,这里js在客户端实现编码和解码 一.在地址栏直接输入:javascript:str=encodeURI("笃行天下& ...

  9. JavaScript基础 -- DOM

    一.DOM全称 文档对象模型(Document Object Model) 二.DOM是什么 DOM可以说是制作动态页面的强有力工具.DOM不是JavaScript语言的一部分,而是内置在浏览器中的一 ...

  10. SNAPSHOTTING

    /etc/redis.conf ################################ SNAPSHOTTING ################################## Sav ...