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 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

Solution:

#include<iostream>
#include<set>

using namespace std;
const int MAX = 9999999;
int Roads[501][501];
bool status[501][501];
set<int>result;
int parent[501];
bool vertexs[501];
set<int> V;
void init_vertexs(int N){
  for(int i = 0; i <= N; i++)
    vertexs[i] = 0;
}
void print_V(){
  cout << "set V : " << endl;
  set<int>::iterator it;
  for(it = V.begin(); it != V.end(); it++)
    cout << *it << endl;
  cout << "--------------------------" << endl;
}
int cal_cost_destory_city(int num_city, int N){
  int root = (num_city == 1) ? 2 : 1;
  //cout << "destory city " << num_city << " choose root " << root << endl; 
  int cur_citys = 1;
  int total_cost = 0;
  init_vertexs(N);
  V.clear();
  vertexs[root] = 1;
  V.insert(root);
  set<int>::iterator it;
  for(int i = 0; i < N; i++){
    int min_road = MAX;
    int min_road_city = 0;
    for(it = V.begin(); it != V.end(); it++){
      for(int j = 1; j <= N; j++){
        //跳过占领的城市
        if(j == num_city)
          continue;
        //未损坏的道路连接的城市直接加入到目前已连通城市当中
        if(Roads[*it][j] != 0 && status[*it][j] && !vertexs[j]){
          // cout << "remain road between " << *it << " and " << j << endl;
          V.insert(j);
          vertexs[j] = 1;
        }else if(Roads[*it][j] != 0 && !status[*it][j] && !vertexs[j] && min_road > Roads[*it][j]){
          min_road_city = j;
          min_road = Roads[*it][j];
        }
      }
    }
    //在损坏的道路当中去最小的 近似于prim算法
    if(min_road_city && !vertexs[min_road_city]){
      //  cout << "min_road = " << min_road<< " add city = " << min_road_city << endl;
      total_cost += min_road;
      V.insert(min_road_city);
      vertexs[min_road_city] = 1;
    }
    //print_V();
    if(V.size() == N - 1)
      break;
  }
  if(V.size() == N - 1)
    return total_cost;
  else
    return MAX;
}

int main(){
  int N, M;
  cin >> N >> M;
  int c1, c2, cost, statu;
  for(int i = 1; i <= M; i++){
    cin >> c1 >> c2 >> cost >> statu;
    Roads[c1][c2] = cost;
    Roads[c2][c1] = cost;
    status[c1][c2] = statu;
    status[c2][c1] = statu;
   }
  if(N <= 2){
    cout << 0 << endl;
    return 0;
  }
  int Most_cost = 0;
  for(int i = 1; i <= N; i++){
    int tem_cost = cal_cost_destory_city(i, N);
    //cout << "destory city " << i << " cost : " << tem_cost << endl;
    if(Most_cost < tem_cost){
      result.clear();
      result.insert(i);
      Most_cost = tem_cost;
    }else if(Most_cost == tem_cost && tem_cost != 0){
      result.insert(i);
    }
  }
  if(!Most_cost)
    cout << 0 << endl;
  else{
    set<int>::iterator it = result.begin();
    cout << *it;
    it++;
    while(it != result.end()){
      cout << " " << *it;
      it++;
    }
    cout << endl;
    }
  return 0;
}

此代码可得正确结果,但对于最后一个测试用例会超时,解决办法可考虑采用链表的方式表示图,减少搜索图的for循环遍历次数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值