1019 - Brush (V)
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【最短路模板】的更多相关文章

  1. Light OJ 1019 - Brush (V)(图论-dijkstra)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...

  2. Light oj 1379 -- 最短路

    In Dhaka there are too many vehicles. So, the result is well known, yes, traffic jam. So, mostly peo ...

  3. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  4. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  5. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  6. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  7. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  8. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  9. k短路模板 POJ2449

    采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. Jquery EasyUI中treegrid的中右键菜单和一般按钮同时绑定事件时的怪异事件

    做个项目使用jquery  easyui来做前端,也许是对此不是很熟悉,总是发现一些不可理解的事件. 主要源代码如下: <script type="text/javascript&qu ...

  2. c语言的笔记

    下面把我这半年来记的一些C语言的笔记贴出来. 1  C语言中函数参数传递是按照“值传递”进行的,即单向传递. 2  函数原型:函数类型 函数名(参数类型,参数类型……),可以不必加参数名,因为操作系统 ...

  3. vs2010 未能正确加载方案中的一个或多个项目

    Visual studio在打开解决方案时,往往会碰到一个这样的错误,提示说:未能正确加载方案中的一个或多个项目: 我们可以通过以下步骤来解决该问题:首先,在相应的sln类型文件上点击右键,选择用记事 ...

  4. tomcat https 配置

    以前基本上笔者对于安全性考虑的并不多,最近因为saas平台要开始逐渐推广,所以需要开始逐渐加强xss/crsf/https等措施以避免潜在的安全性风险.本文简单的记录下tomcat下https的配置. ...

  5. ListToDataTable

    public static DataTable ToDataTable<T>(IEnumerable<T> collection)       {           var ...

  6. C#在声明对象时对其赋值的一种方式

    今天学会一种更方便的赋值方式,如下, 同时存档一个通过 打开对话框 获取地址的方式. private string GetSaveAsPathXls(string defaultFileName) { ...

  7. Django自定义上传目录

    由于数据库的upload_to功能,有时不能满足每次上传灵活自定义的需求, 基于DEF的上传,有时不能满足基于CLASS的视图要求, 于是,只好慢慢用土法实现. 当然,首先,要使用上传功能时,form ...

  8. [转贴]一个将表格变成 INSERT 的SQL 语句的存储过程(sql server)

    来源自http://vyaskn.tripod.com/code.htm#inserts SET NOCOUNT ON GO PRINT 'Using Master database' USE mas ...

  9. 【Xamarin 跨平台机制原理剖析】

    原文:[Xamarin 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原 ...

  10. 转:二十七、Java图形化界面设计——容器(JFrame)

    转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...