hdu2433 bfs+优化

来源:互联网 发布:矩阵计算 袁亚湘 编辑:程序博客网 时间:2024/06/03 09:03

Travel

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


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 45 11 33 25 42 21 21 2
 

Sample Output
INFINFINFINF22
 

Source
2008 Asia Chengdu Regional Contest Online
 

Recommend
lcy
 
思路来自:http://hi.baidu.com/novosbirsk/item/bfcf0cd201edfc2d39f6f709

暴力算法:枚举每条边,然后对每个点求一次单源最短路,由于题目中的边的权值都是1,因此可以用bfs来求得每个点的单源最短路。复杂度是O(M*N*M)。由于M*N*M=3000*100*3000=9*108,不可能AC。

标程算法:仍然使用上面的思路,但要作一些预处理。对每个顶点u求一次单源最短路,把求得的结果称作u的最短路径树,并用数组记录所有点到 其他所有顶点的路径的和。若被破坏的公路不在该最短路径树上,则从u出发的所有最短路径的总和就是u到该树上的所有顶点的路径的总和,因为刚刚记录了这个 数值,因此花费O(1)时间就能返回结果。否则,删除被破坏的公路后,重新通过BFS计算从u出发的所有最短路径的总和,

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define inf 99999999using namespace std;int map[200][200];int sum[200];struct node{    int u,v;};node edge[3005];int pre[200][200];int first;int bfs(int u,int n){    int vis[200];    int dd[200];    memset(vis,0,sizeof(vis));    memset(dd,0,sizeof(dd));    vis[u]=1;    dd[u]=0;    int i;    queue<int> q;    q.push(u);    int count=1;    int s=0;    while(!q.empty())    {        int t=q.front();        q.pop();        for(i=1;i<=n;i++)        {            if(!vis[i]&&map[t][i])            {                dd[i]=dd[t]+1;                if(first)        //只在第一次做,                    pre[u][i]=t; //关键优化,                vis[i]=1;                s+=dd[i];                q.push(i);                count++;            }        }    }    if(count==n)    {        return s;    }    else        return inf;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(map,0,sizeof(map));        memset(sum,0,sizeof(sum));        int i;        memset(pre,0,sizeof(pre));        first=1;        for(i=0;i<m;i++)        {            int a,b;            scanf("%d%d",&a,&b);            map[a][b]++;            map[b][a]++;            edge[i].u=a;            edge[i].v=b;        }        int flag=0;        int ss=0;        for(i=1;i<=n;i++)        {            sum[i]=bfs(i,n);            ss+=sum[i];            if(sum[i]==inf)            {                flag=1;                break;            }        }        if(flag)        {            for(i=0;i<m;i++)                printf("INF\n");            continue;        }        first=0;        for(i=0;i<m;i++)        {            int s=0;            if(map[edge[i].u][edge[i].v]>1)                printf("%d\n",ss);            else if(map[edge[i].u][edge[i].v]==1)            {                int j;                for(j=1;j<=n;j++)                {                    if(pre[j][edge[i].v]!=edge[i].u&&pre[j][edge[i].u]!=edge[i].v)      //关键优化  迅速判断出该边是不是最短树上的路径                        s+=sum[j];                    else                    {                        map[edge[i].u][edge[i].v]--;                        map[edge[i].v][edge[i].u]--;                        s+=bfs(j,n);                        if(s>=inf)                            break;                        map[edge[i].u][edge[i].v]++;                        map[edge[i].v][edge[i].u]++;                    }                }                if(j>n)                    printf("%d\n",s);                else                    printf("INF\n");            }        }    }    return 0;}


原创粉丝点击