Light oj 1019 - Brush (V)【最短路裸题】
2015-08-20 18:35
375 查看
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 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.
PROBLEM SETTER: JANE ALAM JAN
题意:就是求1到N的最短路。裸题
AC代码:
![]() | 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 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 |
题意:就是求1到N的最短路。裸题
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <vector> #include <map> #include <string> #include <algorithm> #define MAXN 100+10 #define MAXM 2000+10 #define INF 0x3f3f3f3f using namespace std; struct Edge { int from, to, val, next; }; Edge edge[MAXM]; int head[MAXN], edgenum; int N, M; void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E = {u, v, w, head[u]}; edge[edgenum] = E; head[u] = edgenum++; } void getMap() { int a, b, c; while(M--) { scanf("%d%d%d", &a, &b, &c); addEdge(a, b, c); addEdge(b, a, c); } } int dist[MAXN]; bool vis[MAXN]; int k = 1; void SPFA(int sx) { queue<int> Q; memset(dist, INF, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[sx] = 0; vis[sx] = true; Q.push(sx); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(dist[E.to] > dist[u] + E.val) { dist[E.to] = dist[u] + E.val; if(!vis[E.to]) { vis[E.to] = true; Q.push(E.to); } } } } printf("Case %d: ", k++); if(dist == INF) printf("Impossible\n"); else printf("%d\n", dist ); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &N, &M); init(); getMap(); SPFA(1); } return 0; }
相关文章推荐
- MySQL学习笔记-事务相关话题
- yum [Errno 256] No more mirrors to try
- Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the ser
- Spring学习笔记——Spring事务只对运行时异常回滚
- 二叉树
- 02-C#(基础)基本的定义和说明
- vs2010 release 模式加了断点,跑代码无法跟踪,解决方法
- EJB中的注释
- Quartz(二)整合Spring容器中bean及动态调度任务
- 1小时内必须解决的5个编程问题
- Java变量及其传递
- HDU 4341 Gold miner (分组背包)
- Java 螺纹第三版 第三章数据同步 读书笔记
- http Clinct
- get和post请求方式
- HDU 2553 N皇后问题 (DFS_回溯)
- Codeforces Round #258 (Div. 2) B. Sort the Array
- 闲话js前端框架(4)——组件化?有没有后端的事?
- 2038问题
- 在spring中使用quartz进行任务调度遇到的问题