PAT「1001 Battle Over Cities - Hard Version (35分)」

1. 题目

题目链接:PAT「1001 Battle Over Cities - Hard Version (35分)」

Description

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 minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.

Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤500), and M, which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.

Note: It is guaranteed that the whole country was connected before the war.

Output Specification:

For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.

In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.

Sample Input 1:

4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 1 0

Sample Output 1:

1 2

Sample Input 2:

4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 2 1

Sample Output 2:

0

2. 题解

分析

直接暴力枚举,考虑去除每个节点后,对剩余的节点使用 Prim 算法计算生成最小生成树所需要的代价,如果不能生成最小生成树,则代价为无穷 INF;依据题意可知,只有 destroyed 的高速公路需要修复代价,即 Status = 0;而 Status = 1 的高速公路不需要修复代价,即修复代价为 0 。由于 Status = 1 的高速公路修复代价都一样,故对这部分高速公路可以直接使用并查集+数组来合并可以合并的联通集;对于 Status = 0 的高速公路的部分,在前者操作的基础上,使用有序数组(按照 cost 从高到底降序)来进一步生成最小生成树,即并查集+有序数组。由于去除了 Status = 1,故进行 sort 操作较快。

由于 N <= 500,故 M <= N*(N-1)/2 = 124750,边 sort 复杂度 O(Mlog(M)),答案 sort 复杂度 O(Nlog(N)),枚举所有节点并构建最小生成树 O(NM)。故最终复杂度为 O(NM)。由于数据不是很大,故「直接枚举+并查集+Prim」算法也跑的挺快的。

代码

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 505, MAXM = 130000;
const int INF = 0x7fff0000;

//边
typedef struct
{
    int u, v;       // u、v 分别为该边的两个端点
}edge;

//点
typedef struct 
{
    int i, cost;    // i 为节点,cost 为去点该节点需要的最小花费 
}ans;

int num_edge;
int father[MAXN], graph[MAXN][MAXN];

bool operator < (const edge e1, const edge e2) {
    return graph[e1.u][e1.v] < graph[e2.u][e2.v];
}

bool operator < (const ans a1, const ans a2) {
    return a1.cost == a2.cost ? a1.i < a2.i : a1.cost > a2.cost;
}

int len_vq;
int len_vpqe;
int len_vpqa;
edge vq[MAXM];
edge vpqe[MAXM];
ans vpqa[MAXN];

// 初始化
void init(int n) {
    num_edge = n-2;
    for(int i = n; i; --i) {
        father[i] = i;
    }
}

// 寻找并查集的根节点
int findfather(int x) {
    return x == father[x] ? x : (father[x] = findfather(father[x]));
}

// 根据已有边尽可能合并节点
void build(int x) {
    for(int i = 0; i < len_vq && num_edge; ++i) {
        if(vq[i].u != x && vq[i].v != x) {
            if(findfather(vq[i].u) != findfather(vq[i].v)) {
                father[father[vq[i].v]] = father[vq[i].u];
                --num_edge;
            }
        }
    }
}

// 根据可修复边合并所有并查集得到最小生成树
int repair(int x) {
    int res = 0;
    for(int i = 0; i < len_vpqe && num_edge; ++i) {
        if(vpqe[i].u != x && vpqe[i].v != x) {
            if(findfather(vpqe[i].u) != findfather(vpqe[i].v)) {
                father[father[vpqe[i].v]] = father[vpqe[i].u];
                --num_edge;
                res += graph[vpqe[i].u][vpqe[i].v];
            }
        }
    }
    if(num_edge) {
        res = INF;
    }
    return res;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = m; i; --i) {
        int x, y, cost, flag;
        scanf("%d%d%d%d", &x, &y, &cost, &flag);
        graph[x][y] = cost;
        if(flag) {
            vq[len_vq++] = edge{x,y};
        } else {
            vpqe[len_vpqe++] = edge{x,y};
        }
    }
    sort(vpqe, vpqe+len_vpqe);
    for(int i = n; i; --i) {
        init(n);
        build(i);
        int res = repair(i);
        vpqa[len_vpqa++] = ans{i, res};
    }
    sort(vpqa, vpqa+len_vpqa);
    if(vpqa[0].cost == 0) {
        printf("0\n");
    } else {
        printf("%d", vpqa[0].i);
        for(int i = 1; i < len_vpqa && vpqa[i].cost == vpqa[0].cost; ++i) {
            printf(" %d", vpqa[i].i);
        }
        printf("\n");
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341