首页 技术 正文
技术 2022年11月12日
0 收藏 532 点赞 2,169 浏览 2048 个字

1019 – Brush (V)

light oj 1019【最短路模板】 PDF (English) Statistics Forum
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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285