lightoj 1002 最小生成树

题意,给出若干条道路(m条)和若干个城市(n个,编号从0~n-1),给出一个起步城市k( 0<=k<=n) , 问所有其他城市到起步城市k的路径中,路径中最长的道路,最短可以 是多少。

分析,虽然一开始看错了题目,当作了最短路做,改了回来,用了并查集+队列拓展。但是Gealo说可以用dij做,数组保存的不是到某点的最短距离,而是到某点的最长的道路长为多少。好像也是可以的。

总之写的是最小生成树,那就用最小生成树写,最小生成树可以完美保证题意,题意中要找到路径,也就是要联通,且最小,符合最小生成树定义。

 1 /* When all else is lost the future still remains. */
 2 #define rep(X,Y,Z) for(int X=(Y);X<(Z);X++)
 3 #define drep(X,Y,Z) for(int X=(Y);X>=(Z);X--)
 4 #define fi first
 5 #define se second
 6 //head
 7 #include <iostream>
 8 #include <stdio.h>
 9 #include <queue>
10 #include <algorithm>
11 using namespace std;
12 #define MAXM 16010
13 #define MAXN 510
14 #define INF 1000000000
15 //int road_val[MAXN][MAXN];
16 pair<int,pair<int,int> > road_val[MAXM];
17 int dis[MAXN][MAXN];
18 int fat[MAXN];
19 int maxdis[MAXN];
20 int dij_dis[MAXN];
21 bool mark[MAXN];
22 void init(){
23     rep(i,0,MAXN) rep(j,0,MAXN) dis[i][j] = INF;
24     rep(i,0,MAXN) fat[i] = i;
25     rep(i,0,MAXN) dij_dis[i] = INF;
26     rep(i,0,MAXN) maxdis[i] = -1;
27     rep(i,0,MAXN) mark[i] = 0;
28     return ;
29 }
30 int find_fa(int x){
31     return fat[x] = (fat[x] == x) ? x : find_fa(fat[x]);
32 }
33 void dij(int pos , int n){
34     queue<int> Q;
35     Q.push(pos);
36     maxdis[pos] = 0;
37     while(!Q.empty()){
38         int now = Q.front();
39         Q.pop();
40         mark[now] = 1;
41         rep(i,0,n){
42             if(mark[i]) continue;
43             if(dis[now][i] >= INF) continue;
44             maxdis[i] = max(maxdis[i],maxdis[now]);
45             maxdis[i] = max(maxdis[i],dis[now][i]);
46             Q.push(i);
47         }
48     }
49     return ;
50 }
51 int main(){
52     int T;
53     scanf("%d",&T);
54     rep(ca,1,T+1){
55         int n , m;
56         scanf("%d %d",&n,&m);
57         init();
58         rep(i,0,m){
59             int u , v , w;
60             scanf("%d %d %d",&u,&v,&w);
61             road_val[i] = make_pair(w,make_pair(u,v));
62         }
63         sort(road_val,road_val+m);
64         rep(i,0,m){
65             int val = road_val[i].fi;
66             int a = road_val[i].se.fi;
67             int b = road_val[i].se.se;
68             int fa = find_fa(a);
69             int fb = find_fa(b);
70             if(fa == fb) continue;
71             fat[fa] = min(fa,fb);
72             fat[fb] = min(fa,fb);
73             dis[a][b] = min(dis[a][b],val);
74             dis[b][a] = min(dis[b][a],val);
75         }
76         int start;
77         scanf("%d",&start);
78         dij(start,n);
79         printf("Case %d:\n",ca);
80         rep(i,0,n){
81             if(maxdis[i] < 0) printf("Impossible\n");
82             else printf("%d\n", maxdis[i]);
83         }
84
85     }
86     return 0;
87 }
时间: 2024-08-07 08:38:00

lightoj 1002 最小生成树的相关文章

二 LightOJ 1002 最小生成树

Description I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0to 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

【LightOJ 1002】 Country Roads

[LightOJ 1002] Country Roads ...又一个另类OJ...最长路 贴个SPFA的 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <set> using namespace std; int mp[555][555]; int dis[555]; bool

lightoj 1029 最小生成树 + 最大生成树

题意,给出若干条连接两个屋子间的路线的价格(保证一定都能联通),问联通所有屋子的最大代价和最小代价的平均值为多少. 分析,即求一次最大生成树,一次最小生成树 1 /* When all else is lost the future still remains. */ 2 #define rep(X,Y,Z) for(int X=(Y);X<(Z);X++) 3 #define drep(X,Y,Z) for(int X=(Y);X>=(Z);X--) 4 #define fi first 5

Lightoj 1002 - Country Roads(prim算法)

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 h

LightOJ 1029 Civil and Evil Engineer最小生成树和最大生成树

F - Civil and Evil Engineer Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1029 Description A Civil Engineer is given a task to connect n houses with the main electric power station directly

1002 搭桥-最小生成树 图论

题目地址:http://codevs.cn/problem/1002/ 这道题考察最小生成树和图的相关知识, 用到了二维到一维的转换(n行m列 坐标转化:(i,j)->  i*m+j ) 进一步利用一维数组做并查集处理,在搭桥过程中 要根据距离来搭桥,搭桥后要进行合并(注意 n个建筑物,n-1座桥就行了). 题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格

LightOj 1123-Trail Maintenance(最小生成树:神级删边)

1123 - Trail Maintenance PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to

Light oj 1002 Country Roads (Dijkstra)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中心城市为t,问每个城市到中心城市的最小路径的花费,路径花费大小的定义为:一条路上花费最大的边的值. 解题思路: Dijkstra的变形,用Dijkstra求出来的单源路径可以保证每条边都是最优的,所以最短路上的最长边就是所求. 1 #include <algorithm> 2 #include &

LightOJ 1380 – Teleport 【最小树形图】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1380 最小树形图也就是有向图的最小生成树.普通的prim无法求解. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <queue> using names