Light oj 1019 - Brush (V)【最短路裸题】
2015-08-20 19:06
281 查看
1019 - Brush (V)
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.
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 Mlines
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 the1st junction
and Atiq lives in the Nth junction. There can be multiple roads between same pair of junctions.
水大一的题
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 Mlines
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 the1st 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> #include <queue> #include <algorithm> #define INF 0x3f3f3f3f #define maxn 100 + 10 #define maxm 2000 + 100 using namespace std; int n, m; struct node { int u, v, w, next; }; node edge[maxm]; int head[maxn], cnt; int dist[maxn], vis[maxn]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w){ edge[cnt] = {u, v, w, head[u]}; head[u] = cnt++; } void getmap(){ scanf("%d%d", &n, &m); while(m--){ int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c); add(b, a, c); } } void SPFA(int x){ queue<int>q; memset(dist, INF, sizeof(dist)); memset(vis, 0, sizeof(vis)); dist[x] = 0; vis[x] = 1; q.push(x); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; int w = edge[i].w; if(dist[v] > dist[u] + w){ dist[v] = dist[u] + w; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } if(dist == INF) printf("Impossible\n"); else printf("%d\n", dist ); } int main (){ int T; scanf("%d", &T); int k = 0; while(T--){ init(); getmap(); printf("Case %d: ", ++k); SPFA(1); } return 0; }
相关文章推荐
- Swift——(三)Swift神奇的下划线
- python学习系列之多线程
- 还是畅通工程
- 在Heroku上,安装Wordpress
- hdoj 1175 (bfs)
- Hdu 2122
- HDOJ 2141 Can you find it? (合并&&二分)
- mongoDB 3.0 安全权限访问控制
- HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)
- python 模块
- linux无锁编程
- Linux下如何查看系统启动时间和运行时间
- Lowest Common Ancestor of a Binary Tree
- android开发环境搭建日记和嵌入式Android开发环境初探
- [iOS]C语言知识点系列视频
- Alex / OverFeat / VGG 中的卷积参数
- 算法初探
- poj 3159 Candies
- 一提到父子类对象
- 项目进度