light oj 1019【最短路模板】
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
Tanvir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found that there is no brush in him room. So, he called Atiq to get a brush. But as usual Atiq refused to come. So, Tanvir decided to go to Atiq's house.
The city they live in is divided by some junctions. The junctions are connected by two way roads. They live in different junctions. And they can go to one junction to other by using the roads only.
Now you are given the map of the city and the distances of the roads. You have to find the minimum distance Tanvir has to travel to reach Atiq's house.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a blank line. The next line contains two integers N (2 ≤ N ≤ 100) and M (0 ≤ M ≤ 1000), means that there are N junctions and M two way roads. Each of the next M lines will contain three integers u v w (1 ≤ u, v ≤ N, w ≤ 1000), it means that there is a road between junction u and v and the distance is w. You can assume that Tanvir lives in the 1st junction and Atiq lives in the Nth junction. There can be multiple roads between same pair of junctions.
Output
For each case print the case number and the minimum distance Tanvir has to travel to reach Atiq's house. If it's impossible, then print 'Impossible'.
Sample Input |
Output for Sample Input |
|
2 3 2 1 2 50 2 3 10 3 1 1 2 40 |
Case 1: 60 Case 2: Impossible |
练练模板
#include<stdio.h>
#include<string.h>
#define MAX 1010
#define INF 0x3f3f3f
int n,m;
int vis[MAX],dis[MAX];
int map[MAX][MAX];
void init()
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=i==j?0:INF;
}
void dijktra()
{
int i,j,next,min;
for(i=1;i<=n;i++)
dis[i]=map[1][i];
memset(vis,0,sizeof(vis));
vis[1]=1;
next=1;
for(i=2;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&min>dis[j])
{
next=j;
min=dis[j];
}
}
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>dis[next]+map[next][j])
dis[j]=dis[next]+map[next][j];
}
}
if(dis[n]==INF)
printf("impossible\n");
else
printf("%d\n",dis[n]);
}
int main()
{
int k,t,j,i,a,b,c;
scanf("%d",&t);
k=0;
while(t--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
printf("Case %d:",++k);
dijktra();
}
return 0;
}
light oj 1019【最短路模板】的更多相关文章
- Light OJ 1019 - Brush (V)(图论-dijkstra)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...
- Light oj 1379 -- 最短路
In Dhaka there are too many vehicles. So, the result is well known, yes, traffic jam. So, mostly peo ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
随机推荐
- demo——06弹性和制作骰子
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 有反斜杠时候,CakePHP往pgsql插入数据异常
原始数据:INSERT INTO “public”.”tables” (“table”, “columns”) VALUES (‘table1\’, ‘{“col1″:false,”col2″:tru ...
- C++相关资源
http://www.cnblogs.com/xi52qian/p/4186983.html语言ISO/IEC JTC1/SC22/WG21 - The C++ Standards Committee ...
- linux下进度条的简单实现
在实现进度条之前,先学习一下makefile. 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中, makefile 定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编 ...
- 内网DMZ外网之间的访问规则
当规划一个拥有DMZ的网络时候,我们可以明确各个网络之间的访问关系,可以确定以下六条访问控制策略. 1.内网可以访问外网 内网的用户显然需要自由地访问外网.在这一策略中,防火墙需要进行源地址转换. 2 ...
- wysiwyg editor
http://www.bootcss.com/p/bootstrap-wysiwyg/
- uva 10652 Board Wrapping
主要是凸包的应用: #include <cstdio> #include <cmath> #include <cstring> #include <algor ...
- Android Studio的一些技巧和使用注意事项(持续更新)
1.创建一个项目之后默认是没有assets目录的,可以手动在main目录下创建一个assets目录. 2.
- 【UVA1331】关于最优三角剖分
最近在练习DP专题,学会了很多表示方法和转换方法,今天做最优三角剖分的时候发现脑子卡了,不会表示状态,于是写个博客记录一下. 最优三角剖分的一类题目都是差不多的.给你一个多边形,让你把它分割成若干个三 ...
- 关于MIUI6下使用Widget调用Toast的一个问题
编写了一个Widget程序,在继承AppWidgetProvider类中调用Toast,发现如下问题: 在小米2,MIUI Version:MIUI5.6.4|Beta, Android Versio ...
