HDU 2433 Travel

Travel

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2840    Accepted Submission(s): 950

Problem Description
      One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
      Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.

Input
      The input contains several test cases.
      The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
      The input will be terminated by EOF.

Output
      Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
 
Sample Input
5 4
5 1
1 3
3 2
5 4
2 2
1 2
1 2
 
Sample Output
INF
INF
INF
INF
2
2
 
思路:预处理,数组mapp存该条先线路出现了多少次,数组used存这条线路被删除会不会对求最短路有影响
 
  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 #define N 105
  5 #define inf 0x3fffffff
  6 using namespace std;
  7 bool used[N][N][N];
  8 int mapp[N][N];
  9 int sum[N],vis[N],dis[N];
 10 int n,m;
 11 struct node{
 12     int x,y;
 13 }point[3010];
 14 void init(){
 15     memset(mapp,0,sizeof(mapp));
 16     memset(sum,0,sizeof(sum));
 17     memset(used,0,sizeof(used));
 18 }
 19 
 20 int bfs(int src,bool mark){
 21     queue<int> q;
 22     int i;
 23     while(!q.empty()){
 24         q.pop();
 25     }
 26     memset(vis,0,sizeof(vis));
 27     memset(dis,0,sizeof(dis));
 28     q.push(src);
 29     vis[src]=1;
 30     while(!q.empty()){
 31         int num=q.front();
 32         q.pop();
 33         for(int i=1;i<=n;i++){
 34             if(!vis[i]&&(mapp[num][i]>0||mapp[i][num]>0)){
 35                 vis[i]=1;
 36                 if(mark){
 37                     used[src][i][num]=true;
 38                     used[src][num][i]=true;
 39                 }
 40                 dis[i]+=dis[num]+1;
 41                 q.push(i);
 42             }
 43         }
 44     }
 45     int tot = 0;
 46     for(int i=1;i<=n;i++){
 47         if(!dis[i]&&i!=src){
 48             return -1;
 49         }
 50         tot+=dis[i];
 51     }
 52     return tot;
 53 }
 54 int main(){
 55     int cnt;
 56     cin.sync_with_stdio(false);
 57     while(cin>>n>>m){
 58         cnt=0;
 59         init();
 60         for(int i=1;i<=m;i++){
 61             cin>>point[i].x>>point[i].y;
 62             mapp[point[i].x][point[i].y]++;
 63             mapp[point[i].y][point[i].x]++;
 64         }
 65         for(int i=1;i<=n;i++){
 66             sum[i]=bfs(i,true);
 67             if(sum[i]==-1){
 68                 cnt=-1;
 69                 break;
 70             }
 71             else{
 72                 cnt+=sum[i];
 73             }
 74         }
 75         for(int i=1;i<=m;i++){
 76             int ans=cnt;
 77             mapp[point[i].x][point[i].y]--;
 78             mapp[point[i].y][point[i].x]--;
 79             if(mapp[point[i].x][point[i].y]>0){
 80                 cout<<ans<<endl;
 81             }
 82             else if(ans==-1){
 83                 cout<<"INF"<<endl;
 84             }
 85             else{
 86                 bool flag=false;
 87                 for(int j=1;j<=n;j++){
 88                     if(used[j][point[i].x][point[i].y]==true){
 89                         ans-=sum[j];
 90                         int tmp=bfs(j,false);
 91                         if(tmp==-1){
 92                             flag=true;
 93                             break;
 94                         }
 95                         ans+=tmp;
 96                     }
 97                 }
 98                 if(flag){
 99                     cout<<"INF"<<endl;
100                 }
101                 else{
102                     cout<<ans<<endl;
103                 }
104             }
105             mapp[point[i].x][point[i].y]++;
106             mapp[point[i].y][point[i].x]++;
107         }
108     }
109     return 0;
110 }
2017-01-25 14:59:48
posted @ 2017-01-25 15:06  ガ落涙『不變』  阅读(291)  评论(0编辑  收藏  举报