Connections between cities&&2009 Multi-University Training Contest 8 - Host by BJNU

29 篇文章 0 订阅
5 篇文章 0 订阅
Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.

Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.

Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.

Sample Input
  
  
5 3 2 1 3 2 2 4 3 5 2 3 1 4 4 5

Sample Output
  
  
Not connected 6
话说这道题,猛一看以为是最短路问题,于是就用spfa写了起来,不一会都写完了,一运行样例都过了,于是就交,却返回来个TLE,那个囧啊,于是看了看题上给的范围,更囧了,spfa,dijstra,各种最短路都会TLE,这怎么办呢,于是想起了前两天学的LCA离线算法,根据题意可知所给的图是无环图,而是一个森林,于是最短路问题就可以完美转发为找最近公共祖先问题。
AC代码:
#include<iostream>
#include<string.h>
#include<cstdio>
#include<string>
#define N 11111
#define M 2100000
#define CLR(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
typedef struct
{
    int num;
    int len;
    int Next;
}Node;
Node s[M];
int ans[M],head[N],head1[N],dis[N],id[N],Father[N];
bool vis[N];
int res,cnt;
void init()
{
    CLR(head,-1);
    CLR(head1,-1);
    CLR(vis,false);
    CLR(id,0);
    CLR(ans,0);
    CLR(Father,0);
    res=0,cnt=0;
}
int Find(int x)
{
    if(x==Father[x]) return x;
    return Father[x]=Find(Father[x]);
}
void add(int a,int b,int c,int h[])
{
    s[res].num=b,s[res].len=c,s[res].Next=h[a],h[a]=res++;
    s[res].num=a,s[res].len=c,s[res].Next=h[b],h[b]=res++;
}
void Tarjan(int x)
{
    id[x]=cnt;
    int v;
    vis[Father[x]=x]=true;
    for(int i=head1[x];i!=-1;i=s[i].Next)
        if(vis[v=s[i].num])
        {
            if(id[v]==id[x]) ans[s[i].len]=dis[x]+dis[v]-2*dis[Find(v)];
            else  ans[s[i].len]=-1;
        }
        for(int i=head[x];i!=-1;i=s[i].Next)
         if(!vis[v=s[i].num])
         {
             dis[v]=dis[x]+s[i].len;
             Tarjan(v);
             Father[v]=x;
         }
}
int main()
{
    int n,m,c;
    while(~scanf("%d%d%d",&n,&m,&c))
    {
        init();
        for(int i=0;i!=m;++i)
        {
            int a,b,d;
            scanf("%d%d%d",&a,&b,&d);
            add(a,b,d,head);
        }
        for(int i=1;i<=c;++i)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(a,b,i,head1);
        }
        for(int i=1;i<=n;++i,cnt++)
            if(!vis[i]) dis[i]=0,Tarjan(i);
        for(int i=1;i<=c;++i)
            if(ans[i]<0) puts("Not connected");
            else  printf("%d\n",ans[i]);
    }return 0;
}

spfa算法时间复杂度为O(kE),k大约为2,但是spfa是由bellmanfloyd改进而来,所以给算法在一定得情况下会退化到O(E*N)加优先队列优化的dijstra算法的时间复杂度为ElogN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值