最小生成树
链接: https://pintia.cn/problem-sets/994805148990160896/problems/994805156657348608
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 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
第一次尝试
一开始是想用dfs解法,分别对每个点dfs,将每两个连通分量用ways中的边连起来,同时加上它的cost,直到counter == N - 1。 再找到最大值存储于vector内,但是运行超时了。
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX_COS 9999999
using namespace std;
struct Highway {
int city_a;
int city_b;
int cost;
};
vector<vector<int> > edge;
vector<Highway> ways;
vector<bool> isFinded;
int counter = 0;
bool way_cmp(Highway a, Highway b) {
return a.cost < b.cost;
}
void DFS(int now_city) {
isFinded[now_city] = true;
counter++;
for (int i = 0; i<edge[now_city].size(); i++) {
int next_city = edge[now_city][i];
if (!isFinded[next_city]) {
DFS(next_city);
}
}
}
int find_cost(int destroy_city, int N) {
fill(isFinded.begin(), isFinded.end(), 0);
isFinded[destroy_city] = true;
counter = 0;
int cost = 0;
int start = 1;
if (destroy_city == 1)
start++;
while (counter < N - 1) {
DFS(start);
if (counter < N - 1) {
for (int i = 0; i<ways.size(); i++) {
if (ways[i].city_a == destroy_city || ways[i].city_b == destroy_city)
continue;
if (isFinded[ways[i].city_a] && !isFinded[ways[i].city_b]) {
start = ways[i].city_b;
cost += ways[i].cost;
break;
}
else if (isFinded[ways[i].city_b] && !isFinded[ways[i].city_a]) {
start = ways[i].city_a;
cost += ways[i].cost;
break;
}
}
if (isFinded[start]) {
return MAX_COS;
}
}
else {
break;
}
}
return cost;
}
int main(int argc, char const *argv[])
{
int N, M;
cin >> N >> M;
edge.resize(N + 1);
isFinded.resize(N + 1, 0);
for (int i = 0; i < M; i++)
{
Highway hw;
int flag;
cin >> hw.city_a >> hw.city_b >> hw.cost >> flag;
if (flag == 0) {
ways.push_back(hw);
}
else {
edge[hw.city_a].push_back(hw.city_b);
edge[hw.city_b].push_back(hw.city_a);
}
}
sort(ways.begin(), ways.end(), way_cmp);
vector<int> maxCostCity;
int max_cost = 0;
for (int city = 1; city <= N; city++)
{
int temp_cost = find_cost(city, N);
if (temp_cost == 0) {
continue;
}
else if (temp_cost > max_cost) {
max_cost = temp_cost;
maxCostCity.clear();
maxCostCity.push_back(city);
}
else if (temp_cost == max_cost) {
maxCostCity.push_back(city);
}
}
if (maxCostCity.size() == 0) {
cout << "0" << endl;
}
else {
cout << maxCostCity[0];
for (int i = 1; i < maxCostCity.size(); i++)
cout << " " << maxCostCity[i];
cout << endl;
}
return 0;
}
然后就运行超时了:
第二次尝试
用最小生成树得到是否联通,精妙之处在于1. 对所有edge进行排序,把status==0的放在后面;2. 用一个 fath[] 数组记录当前节点的最根节点,以判断是否需要这条边的加入。
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX_COST 999999
using namespace std;
struct edge {
int city_a, city_b, cost, avali;
bool operator <(edge &s) const {
return avali != s.avali ? avali > s.avali : cost < s.cost;
}
void input() {
scanf("%d %d %d %d", &city_a, &city_b, &cost, &avali);
}
};
vector<edge> ve;
vector<int> fath, co;
int getfath(int x)
{
return x == fath[x] ? x : fath[x] = getfath(fath[x]);
}
int main() {
int cityNum, edgeNum;
scanf("%d %d", &cityNum, &edgeNum);
ve.resize(edgeNum);
fath.resize(cityNum + 1);
co.resize(cityNum + 1, 0);
for (int i = 0; i < edgeNum; i++)
ve[i].input();
sort(ve.begin(), ve.end());
int maxres = 0;
vector<int> ans;
for (int i = 1; i <= cityNum; i++) {//生成最小生成树
int counter = 1;
for (int j = 1; j <= cityNum; j++)//记录当前节点的根节点
fath[j] = j;
for (int j = 0; j < ve.size(); j++) {
int a = ve[j].city_a, b = ve[j].city_b;
if (a == i || b == i)//去除失陷的城市节点
continue;
if (getfath(a) == getfath(b))//这条边是不需要的
continue;
if(ve[j].avali == 0)//加上这条边的开销
co[i] += ve[j].cost;
counter++;
fath[getfath(a)] = fath[getfath(b)];
}
if (counter < cityNum - 1)
co[i] = MAX_COST;
if (maxres < co[i]) {
ans.clear();
ans.push_back(i);
maxres = co[i];
}
else if (maxres == co[i])
ans.push_back(i);
}
if (maxres == 0)
cout << 0 << endl;
else {
cout << ans[0];
for (int i = 1; i < ans.size(); i++) {
cout << " " << ans[i];
}
cout << endl;
}
system("pause");
return 0;
}