bonus02. Battle Over Cities - Hard Version (20)

来源:互联网 发布:淘宝详情免费添加视频 编辑:程序博客网 时间:2024/06/18 15:03

1001. Battle Over Cities - Hard Version (35)

时间限制
800 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 51 2 1 11 3 1 12 3 1 02 4 1 13 4 1 0
Sample Output 1:
1 2
Sample Input 2:
4 51 2 1 11 3 1 12 3 1 02 4 1 13 4 2 1
Sample 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 function2.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 parameterint cost1[501];//to store the min price for every nodeNode* 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 graphreturn(Find(&No[c.node_first])==Find(&No[c.node_second]));}void unino(edge &c){//function unino two node to a disjoin setFind(&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++) //initialationNo[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 treewhile ((count < maxN - 2)) {while (ed[pointer].node_first == N || ed[pointer].node_second == N || iscircle(ed[pointer])) {//to find the smallest edgepointer++;if (pointer == maxM)//if there is no edge fit the needbreak;}if (pointer == maxM)//note: there has a (IF) to handle the situation that maxN=1break;//add the edge to the min spin treeunino(ed[pointer]); //unino the node into the disjoin setif (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 edgescin >> N >> M;edge *ed = new edge[M + 1];int i;//initialationint temp1, temp2, temp3, temp4;//just for inputing the datafor (i = 0; i < N; i++) {No[i].ID = i + 1;No[i].father = NULL;}//end initialation//inputfor (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 edgeed[i].node_second = temp2;//the order nodeed[i].state = temp4;}//end input//handle the datasort(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 algorithmfor (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 priceif (cost1[i + 1] > max)max = cost1[i + 1];}//outputif (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 lineprintf(" ");elsettt=1;printf("%d",i);}//end output}

感想:
1.kruskal+路径压缩的并查集可以不超时
2.这是贵校的bonus,dalao们自行绕道反正我已经交过了=.=
3.英语不好见谅,我这个算法还是比较快的相比于其他的还是比较快的,测试例子最多用70ms就通过了
4.也可以prim+并查集或者先生成最小树,算的时候把需要求的点相关的边去掉再求树

提交代码
0 0
原创粉丝点击