poj 2485 求最小生成树中 最大的一个权值

Sample Input

1 //T

3 //n
0 990 692 //邻接矩阵
990 0 179
692 179 0
Sample Output

692

prim

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=510;
11 bool vis[MAXN];
12 int lowc[MAXN];
13 int n ;
14 int cost[MAXN][MAXN] ;
15 int MAX = 0 ;
16
17 int Prim()//点是0~n-1
18 {
19     int ans=0;
20     memset(vis,false,sizeof(vis));
21     vis[0]=true;
22     for(int i=1;i<n;i++)lowc[i]=cost[0][i];
23     for(int i=1;i<n;i++)
24     {
25         int minc=INF;
26         int p=-1;
27         for(int j=0;j<n;j++)
28             if(!vis[j]&&minc>lowc[j])
29             {
30                 minc=lowc[j];
31                 p=j;
32             }
33             if(minc==INF)return -1;//原图不连通
34             ans+=minc;
35             if (minc > MAX)
36                 MAX = minc ;
37             vis[p]=true;
38             for(int j=0;j<n;j++)
39                 if(!vis[j]&&lowc[j]>cost[p][j])
40                     lowc[j]=cost[p][j];
41     }
42     return ans;
43 }
44
45 int main()
46 {
47
48    // freopen("in.txt","r",stdin) ;
49     int T ;
50     cin>>T ;
51     while(T--)
52     {
53         cin>>n ;
54         int w ;
55         int i , j ;
56         for (i = 0 ; i < n ; i++)
57             for (j = 0 ; j < n ; j++)
58                {
59                    cin>>w ;
60                    if(w==0)
61                      cost[i][j] = INF ;
62                    else
63                      cost[i][j] = w ;
64                }
65         MAX = 0 ;
66         Prim() ;
67         cout<<MAX<<endl ;
68
69     }
70     return 0 ;
71 }

时间: 2024-08-28 08:01:09

poj 2485 求最小生成树中 最大的一个权值的相关文章

POJ #2485 Highways MST中的最大边权

Description 问题描述:链接 思路 题目很直接,容易看出建的图是一张完全图,需要求出图中最小生成树的最大边权.由于数据上界已经定死了,那么选择用静态邻接表存建图.由于图是稠密图,那么求MST的话就选择 prim . 做完 POJ #2253 Frogger 变种Dijkstra 后再做这个就很有感觉了.其实prim中的核心操作就是记录并更新点v到树的最短距离,然后把所有最短距离之中的最小值选出,将该点其加入树集.这个树集与dijkstra中的点集是异曲同工的. 能用 scanf 的话尽

POJ 2485 Highways (求最小生成树中最大的边)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways

HDU 2485 求删最少点使得 边权=1的有向图最短路&gt;k

题意: 给定n个点 m条有向边 k 下面m条有向边 问删最少几个点使得1-n的最短路>k 10 11 5 1 2 2 3 3 4 4 5 5 10 2 9 1 6 6 7 7 8 8 9 9 10 8 10 5 1 2 2 3 3 4 4 5 5 6 6 8 1 7 7 8 4 7 7 4 #include <stdio.h> #include <string.h> #define N 55 #define INF 1<<30 #define eps 1e-5 i

poj 2349 求MST中第S大的权值

题目大意: 有一些炮台,如果这个炮台有卫星接收器,那么任意两个有卫星接收器的炮台可以通信,不受距离限制:否者,两个炮台之间只能通过对讲机通信,这是受距离限制的.要买一种对讲机,用在需要的炮台上,要求所有炮台两两之间可以直接或者间接通信,问要买通信距离D至少为多少的对讲机可以满足要求. 有S个卫星接收器,那么就可以减少S-1个距离开销.要让D尽可能小,就让这S-1个距离开销最大,所以,想法就是,求这些点的最小生成树,然后把所选的边排序,第S大的边的权值就是所求.假如有4个点,2个卫星,那么最长的那

POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23426   Accepted: 10829 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

POJ 2253 Frogger(Dijkstra变形——最短路径最小权值)

题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' suns

POJ 2485 Highways 最小生成树 (Kruskal)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i

poj 2559求柱形图中最大矩形

两种解法.当中一种是用单调栈. 我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形.所以求出每一个包括矩形histogram[i]的最大矩形的面积.输出这些面积中最大那个就可以. key:用两个数组记录histogram[i]左右两边第一个比它小的单位矩形的序号leftLowerId[i]和rightLowerId[i].那么对于histogram[i],它自己的最大矩形面积就是(rightLowerId[i] - leftLowerId[i] - 1) *  histogram[i]