LightOJ 1019-Brush (V)【最短路,模板题】
来源:互联网 发布:文字游戏安卓制作软件 编辑:程序博客网 时间:2024/05/22 09:38
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 theNth 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
解题思路:
就是求从1这个起点到n这个终点的最短距离。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m;int map[200][200];bool vis[200];int dis[200];const int INF=0x3f3f3f3f;void init(){int i,j;for(i=1;i<=150;i++){for(j=1;j<=150;j++){if(i==j)map[i][j]=0;elsemap[i][j]=INF;}}}void f(int x){int i,j;memset(vis,false,sizeof(vis));for(i=1;i<=n;i++){dis[i]=map[x][i];}vis[x]=true;dis[x]=0;for(i=1;i<n;i++){int p=x,min=INF;for(j=1;j<=n;j++){if(!vis[j]&&dis[j]<min){p=j;min=dis[j];}}vis[p]=true;for(j=1;j<=n;j++){if(!vis[j]&&dis[j]>dis[p]+map[p][j]){dis[j]=dis[p]+map[p][j];}}}if(dis[n]==INF){printf("Impossible\n");}else{printf("%d\n",dis[n]);}}int main(){int t;scanf("%d",&t);int cc=1;while(t--){scanf("%d%d",&n,&m);init();int i,j;while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);if(map[u][v]>w){map[u][v]=map[v][u]=w;}}printf("Case %d: ", cc++);f(1); }return 0;}
- LightOJ 1019-Brush (V)【最短路,模板题】
- [最短路+水题]LightOJ 1019 - Brush (V)
- LIGHTOJ 1019 – BRUSH (V) 【最短路】
- LightOJ Brush (V) 1019【简单最短路】
- lightoj 1019 - Brush (V) 【最短路 floyd】
- LightOJ - 1019 Brush (V) (最短路) 水
- LightOJ 1019 Brush (V) 最短路裸题
- Light oj 1019 - Brush (V)【最短路裸题】
- Light oj 1019 - Brush (V)【最短路裸题】
- LightOJ - 1019 Brush (V)
- LightOJ 1019 Brush (V)
- lightOJ 1019 - Brush (V)
- LightOJ-1019-Brush (V)
- LightOJ - 1019 Brush (V)(floyd)
- Lightoj 1019 - Brush (V)【dijkstra+spfa】
- Lightoj1019——Brush (V)(最短路)
- hdu1869_最短路模板题
- hdu1874 最短路模板题
- 中间件技术(摘抄)
- 视频压缩编码和音频压缩编码的基本原理
- [Interactive Python]Practice Exercises for Functions 解答
- 如何使UICollectionView的Header悬浮
- Redis常用命令
- LightOJ 1019-Brush (V)【最短路,模板题】
- 个人喜欢的关于模式识别、机器学习、推荐系统、图像特征等方面个人主页及博客
- exe4j的使用
- 构造函数中不要调用虚函数
- mybatis在xml文件中处理大于号小于号的方法
- JAVA——多线程Thread
- java反射
- SM4
- iOS UIImageView 动图