BZOJ1083 繁忙的都市

Description

  城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道
路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连
接。这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了。每条道路都有一个分值,分值越小表示这
个道路越繁忙,越需要进行改造。但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的
要求: 1. 改造的那些道路能够把所有的交叉路口直接或间接的连通起来。 2. 在满足要求1的情况下,改造的
道路尽量少。 3. 在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小。任务:作为市规划
局的你,应当作出最佳的决策,选择那些道路应当被修建。

Input

  第一行有两个整数n,m表示城市有n个交叉路口,m条道路。接下来m行是对每条道路的描述,u, v, c表示交叉
路口u和v之间有道路相连,分值为c。(1≤n≤300,1≤c≤10000)

Output

  两个整数s, max,表示你选出了几条道路,分值最大的那条道路的分值是多少。

Sample Input

4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

Sample Output

3 6

 

 

正解:kruskal最小生成树算法

解题报告:

  大概题意是求使图成为连通图的最小代价

  又是一道模板水题,最小生成树直接水过。

//It is made by jump~
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n,m,total,maxl;
int father[311];
 
struct node{
        int u,to,w;  
}jump[300011];
 
int getint()
{
       int w=0,q=0;
       char c=getchar();
       while((c<'0' || c>'9') && c!='-') c=getchar();
       if (c=='-')  q=1, c=getchar();
       while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
       return q ? -w : w;
}
  
bool cmp(node a,node b) { return a.w<b.w; }
  
int find(int x){
      if(father[x]!=x)  father[x]=find(father[x]);
      return father[x];    
} 
 
void hebing(int x,int y){
      father[y]=x;   
}
 
int main()
{
    n=getint();m=getint();
    for(int i=1;i<=m;i++) {
        jump[i].u=getint();jump[i].to=getint();jump[i].w=getint();        
    }
    for(int i=1;i<=n;i++) father[i]=i;
    sort(jump+1,jump+m+1,cmp);
    for(int i=1;i<=m;i++){
        int r1=find(jump[i].u),r2=find(jump[i].to);
        if(r1!=r2) {
            hebing(r1,r2);
            if(jump[i].w>maxl) maxl=jump[i].w;
            total++;           
        }
    }
     
    printf("%d %d",total,maxl);
    return 0;
}

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/5493996.html