lightoj 1002 - Country Roads(最短路变形)
来源:互联网 发布:汉字域名百度收录吗 编辑:程序博客网 时间:2024/06/08 12:23
I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you have to find the minimum cost to go to my city. The cost is defined by the cost of the maximum road you have used to go to my city.
For example, in the above picture, if we want to go from 0 to 4, then we can choose
1) 0 - 1 - 4 which costs 8, as 8 (1 - 4) is the maximum road we used
2) 0 - 2 - 4 which costs 9, as 9 (0 - 2) is the maximum road we used
3) 0 - 3 - 4 which costs 7, as 7 (3 - 4) is the maximum road we used
So, our result is 7, as we can use 0 - 3 - 4.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with a blank line and two integers n (1 ≤ n ≤ 500) and m (0 ≤ m ≤ 16000). The next m lines, each will contain three integersu, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 20000) indicating that there is a road between u and v with cost w. Then there will be a single integert (0 ≤ t < n). There can be multiple roads between two cities.
Output
For each case, print the case number first. Then for all the cities (from 0 to n-1) you have to print the cost. If there is no such path, print'Impossible'.
Sample Input
Output for Sample Input
2
5 6
0 1 5
0 1 4
2 1 3
3 0 7
3 4 6
3 1 8
1
5 4
0 1 5
0 1 4
2 1 3
3 4 7
1
Case 1:
4
0
3
7
7
Case 2:
4
0
3
Impossible
Impossible
Note
Dataset is huge, user faster I/O methods.
一开始忘记将边的k值初始,然后各种TLE,RE,找了半天错误,吃一堑长一智。。。
#include<bits/stdc++.h>using namespace std;const int maxn = 5005;const int maxm = 500000;const int INF = 0x3f3f3f3f;int n, m, k, ca = 1, head[maxn], dis[maxn];bool book[maxn];struct node{int v, w, next;}edge[maxm];void addEdge(int u, int v, int w){edge[k].v = v;edge[k].w = w;edge[k].next = head[u];head[u] = k++;}void spfa(int u){memset(book, 0, sizeof(book));for(int i = 1; i <= n; i++) dis[i] = INF;queue<int> q;q.push(u);dis[u] = 0;book[u] = 1;while(!q.empty()){u = q.front(); q.pop();book[u] = 0;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;int w = edge[i].w;if(dis[v] > max(dis[u], w))<span style="white-space:pre"></span>//与最短路不太一样的地方{dis[v] = max(dis[u], w);if(!book[v]) book[v] = 1, q.push(v);}}}printf("Case %d:\n", ca++);for(int i = 1; i <= n; i++)if(dis[i] == INF) puts("Impossible");else printf("%d\n", dis[i]);}int main(void){int t, s;cin >> t;while(t--){memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);while(m--){int u, v, w;scanf("%d%d%d", &u, &v, &w);u++, v++;addEdge(u, v, w);addEdge(v, u, w);}scanf("%d", &s);spfa(s+1);}return 0;}
- 【最短路】 LightOJ 1002 - Country Roads 变形
- LightOJ 1002 Country Roads 最短路变形
- lightoj 1002 - Country Roads(最短路变形)
- LightOJ 1002 Country Roads(最短路变形(SPFA||贝尔曼))
- LightOJ-1002-Country Roads [最短路][Dijkstra]
- Lightoj1002——Country Roads(最短路变形)
- LightOJ1002---Country Roads (最短路变形)
- lightoj 1002 Country Roads (Dijkstra变形)
- lightOJ 1002:Country Roads
- LightOj 1002 - Country Roads
- 【LightOJ 1002】 Country Roads
- LightOJ 1002 Country Roads
- 1002 - Country Roads(lightoj)
- 【Dijkstra】-LightOJ-1002-Country Roads
- poj1724--ROADS(最短路变形)
- poj-ROADS(最短路变形)
- LightOJ - 1002 Country Roads(最小瓶颈路)
- Light oj 1002 - Country Roads 迪杰斯特拉变形
- Swift 3.0
- rmq-st算法<区间最大最小>(hdu 5875)
- idea不支持泛型
- MSSQL_连接异常_网络连接异常(无法连接到服务器)_无法连接到WMI提供程序
- Logcat查看/输出日志信息
- lightoj 1002 - Country Roads(最短路变形)
- stack的数组实现(结构体封装版)
- 笔试选择题二十八
- 虚函数
- Linux中如何替换文件名中的空格
- POJ - 1742 Coins(多重背包)@
- 对象如何持久化(用json_encode)?
- Python-2
- Matlab中二进制数据文件的读写