PAT-Battle Over Cities - Hard Version (35 分)-最小生成树

这个题的题意是假设某个城市被占领后,要使剩下的城市保持联通可能会花钱修路,求最小花费里花费最多的那个被占领的城市。

这个题凭感觉就是最小生成树,最小生成树满足权值最小(最小花费),所以依次去掉某个城市的所有与其相接的路径,把剩下的路加入最小生成树,求最大值即可。

有一个地方写的时候没注意到,就是去掉某个城市后可能导致连通块的数量的增多,这种情况下算这个城市的花费无限大就可以了(自己真菜QAQ)。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

const int INF = 0x3f3f3f3f;
int n,m,max1;
int f[505];
vector<int> ans;

struct P{
    int u,v,c,s;
    bool operator<(const P &a)const{
        if(s != a.s) return s > a.s;//优先选择还能用的路,这种路径不增加花费
        else return c<a.c;//其实是处理需要修理的路径,先修花费少的
    }
}p[250005];

int Find(int x){
    if(x==f[x]) return x;
    else return f[x]=Find(f[x]);
}

int main(){
    scanf("%d%d",&n,&m);
    max1=0;
    for(int i=1;i<=m;i++)
        scanf("%d%d%d%d",&p[i].u,&p[i].v,&p[i].c,&p[i].s);
    sort(p+1,p+1+m);
    for(int i=1;i<=n;i++){
        int num=0,cnt=0;
        for(int i=1;i<=n;i++) f[i]=i;
        for(int j=1;j<=m;j++){
            if(num==n-2) break;
            int u=p[j].u;
            int v=p[j].v;
            if(u==i || v==i) continue ;//去掉某个城市
            u=Find(u);
            v=Find(v);
            if(u!=v){
                f[u]=v;
                num++;
                if(p[j].s==0) cnt+=p[j].c;
            }
        }
        if(num!=n-2){//先判断删除某个城市后是否连通
            if(max1!=INF){
                max1=INF;
                ans.clear();
            }
            ans.push_back(i);
        }else{
            if(cnt>max1){
                max1=cnt;
                ans.clear();
                ans.push_back(i);
            }else if(cnt==max1 && max1!=0)
                ans.push_back(i);
        }
    }
    if(ans.size()==0) printf("0\n");
    else{
        sort(ans.begin(),ans.end());
        printf("%d",ans[0]);
        for(int i=1;i<ans.size();i++)
            printf(" %d",ans[i]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/JustDoA/p/11180592.html

时间: 2024-10-05 03:09:53

PAT-Battle Over Cities - Hard Version (35 分)-最小生成树的相关文章

pat 顶级 1001 Battle Over Cities - Hard Version (35 分)

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the

PAT-Top1001. Battle Over Cities - Hard Version (35)

在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要.如果剩下的城市无法互通,则说明代价无限大,这个城市至关重要.最后输出的是代价最大的城市序号有序列表.借助并查集和Kruskal算法(最小生成树算法)来解决这个问题. 1 //#include "stdafx.h" 2 #include <iostream> 3 #include

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

pat1001. Battle Over Cities - Hard Version 解题报告

/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+最小堆求出最小生成树 kruskal1.之前图中未破坏的边必用,全部加到图中2.途中被破坏的边按照边权从小到大的顺序依次加入图中,直到图变为连通图 两个方法的对应一个点的最小生成树的复杂度都是nlogm,第二个方法较好写 优化:1.未破坏的边直接加入图中2.当有n-2条边(当前删去一个点后,图中n-

「日常训练」Battle Over Cities - Hard Version(PAT-TOP-1001)

题意与分析 题意真的很简单,实在不想讲了,简单说下做法吧. 枚举删除每个点,然后求最小生成树,如果这个路已经存在那么边权就是0,否则按照原来的处理,之后求花费,然后判整个图是否联通(并查集有几个root),如果不联通直接硬点花费是INF,然后处理输出答案即可. 一道最小生成树的模板题,比较有学习的意义. 代码 /* * Filename: pat_top_1001.cpp * Date: 2018-11-05 */ #include <bits/stdc++.h> #define INF 0x

pat 1013 Battle Over Cities(25 分) (并查集)

1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth

1013 Battle Over Cities (25分) DFS | 并查集

1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe

1013. Battle Over Cities (25)——PAT (Advanced Level) Practise

题目信息: 1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward th