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 0Sample Output 1:
1 2Sample Input 2:
4 5 1 2 1 1 1 3 1 1 2 3 1 0 2 4 1 1 3 4 2 1Sample Output 2:
0
/*********************************
@name:********
@student_ID:314*****
@time:2017.1.1
@bonus2:battle over city
@note: Finally I pass the test with krusual alrorithm
@i use min spin tree to solve the problem. if the price is INF , than we just can not bulid the complete minspin tree.
@step:
1.sort the edge, the distoryed one has the lower priority.
1.1 we don't have to use heap to get the min edge, because we donot have the processes to add the edge, just use qsort or sort function
2.for every note, use krusual algorithm to find the min price, then store it in an array.
2.1 we have to use disjoin set to get the node in the min spin tree(just in kruskal algorithm)
3.transverse the array to find the answer note.
*********************************/
#include<iostream>
#include<algorithm>
using namespace std;
class edge {//edge class,the same struct as input data.
public:
int cost;
int node_first;
int node_second;
int state;//0=distroy,1=inuse.
};
bool compare1(const edge &a,const edge &b){//compare function for STL_sort algorithm.
if (a.state != b.state)
return a.state > b.state;
if (a.node_first != b.node_first)
return a.node_first < b.node_first;
return a.cost < b.cost;
}
class Node {//class node (disjoint set)
public:
int ID;
Node *father;
};
Node No[503];//to store the node parameter
int cost1[501];//to store the min price for every node
Node* Find(Node *c){// find with path compression
if(c->father==NULL)
return c;
return c->father=Find(c->father);//path compression
}
bool iscircle(edge &c) {//function return whether the two node are in a circle in graph
return(Find(&No[c.node_first])==Find(&No[c.node_second]));
}
void unino(edge &c){//function unino two node to a disjoin set
Find(&No[c.node_first])->father= Find(&No[c.node_second]);
}
void Kruskal(edge *ed, int N, int maxN, int maxM, int t) {//kruskal algorithm with disjoint set
int i;
for (i = 1; i < maxN + 1; i++) //initialation
No[i].father = NULL;
int sum = 0;
int pointer = 0;
int count = 0;
while (ed[pointer].node_first == N || ed[pointer].node_second == N)//if the edge has Node N
pointer++;
unino(ed[pointer]);
if (ed[pointer].state == 0)
sum += ed[pointer].cost;
pointer++;
count++;//for counting the edge in min spin tree
while ((count < maxN - 2)) {
while (ed[pointer].node_first == N || ed[pointer].node_second == N || iscircle(ed[pointer])) {//to find the smallest edge
pointer++;
if (pointer == maxM)//if there is no edge fit the need
break;
}
if (pointer == maxM)//note: there has a (IF) to handle the situation that maxN=1
break;
//add the edge to the min spin tree
unino(ed[pointer]); //unino the node into the disjoin set
if (ed[pointer].state == 0)
sum += ed[pointer].cost;
count++;
if (++pointer == maxM)
break;
}
cost1[t] = sum;
if (count != maxN - 2)
cost1[t] = 1000000000;//define 1000000000 as INF
}
int main() {
int N, M;//N==number of vertices, M==number of edges
cin >> N >> M;
edge *ed = new edge[M + 1];
int i;
//initialation
int temp1, temp2, temp3, temp4;//just for inputing the data
for (i = 0; i < N; i++) {
No[i].ID = i + 1;
No[i].father = NULL;
}
//end initialation
//input
for (i = 0; i < M; i++) {
scanf("%d %d %d %d", &temp1, &temp2, &temp3, &temp4);
ed[i].cost = temp3;
ed[i].node_first = temp1;//the one node in an edge
ed[i].node_second = temp2;//the order node
ed[i].state = temp4;
}
//end input
//handle the data
sort(ed,ed+M,compare1);//sort the record by compare1 function
//note: in conpare1 function the parameter sended in must in(const edge&)type
//Kruskal part of algorithm
for (i = 0; i < N; i++)
Kruskal(ed, i + 1, N, M, i + 1);
int max = -1, ttt = 0;
for (i = 0; i < N; i++) {//find the max price
if (cost1[i + 1] > max)
max = cost1[i + 1];
}
//output
if (max == 0) {
cout << 0;
return 0;
}
for (i = 0; i < N + 1; i++)
if (cost1[i] == max) {
if (ttt == 1)//to make sure that there has no extra space at the end of the line
printf(" ");
else
ttt=1;
printf("%d",i);
}
//end output
}
4.也可以prim+并查集或者先生成最小树,算的时候把需要求的点相关的边去掉再求树
提交代码