hdu 2874

来源:互联网 发布:于正 知乎 编辑:程序博客网 时间:2024/06/06 07:14

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9128    Accepted Submission(s): 2199


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 21 3 22 4 35 2 31 44 5
 

Sample Output
Not connected6
这题要判断两个点是否连通,把dist初始化为-1 就可以判断俩个点是否连通因为只有联通的点才能够更改dist的值,是个技巧点
这题对内存的要求很严格,res不用开long long 边的的数组两倍就可以了
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int N = 10001;typedef long long LL;struct node1{    int to, wight, next;}p1[2*N];struct node2{    int to, index, next;}p2[200*N];int head1[N], head2[N], visit[N], par[N];int dist[N], res[100*N];int cnt1, cnt2;void init();int n, m, q;void add1(int u,int v,int c);void add2(int u,int v,int c);void tarjan(int u);int ser(int x);int main(){    while(scanf("%d %d %d", &n, &m, &q)!=EOF)    {        init();        int x, y, cost;        for(int i=1;i<=m;i++)        {            scanf("%d %d %d", &x, &y, &cost);            add1(x,y,cost);            add1(y,x,cost);        }        for(int i=1;i<=q;i++)        {            scanf("%d %d", &x, &y);            add2(x,y,i);            add2(y,x,i);        }        for(int i=1;i<=n;i++)        {            if(!visit[i])            {                memset(dist,-1,sizeof(dist));                dist[i]=0;                tarjan(i);            }        }        for(int i=1;i<=q;i++)        {            if(res[i]==-1)            {                printf("Not connected\n");            }            else            {                printf("%d\n",res[i]);            }        }    }    return 0;}void init(){    memset(head1,-1,sizeof(head1));    memset(head2,-1,sizeof(head2));    memset(visit, 0, sizeof(visit));    for(int i=1;i<=n;i++)    {        par[i]=i;    }    cnt1=0,cnt2=0;    return ;}void add1(int u,int v,int c){    p1[cnt1].to=v;    p1[cnt1].wight=c;    p1[cnt1].next=head1[u];    head1[u]=cnt1++;    return ;}void add2(int u,int v,int c){    p2[cnt2].to=v;    p2[cnt2].index=c;    p2[cnt2].next=head2[u];    head2[u]=cnt2++;    return ;}void tarjan(int u){    visit[u]=1;    for(int i=head1[u];i!=-1;i=p1[i].next)    {        int v=p1[i].to;        if(!visit[v])        {            dist[v]=dist[u]+p1[i].wight;            tarjan(v);            par[v]=u;        }    }    for(int i=head2[u];i!=-1;i=p2[i].next)    {        int v=p2[i].to;        if(visit[v]&&dist[v]!=-1)        {            res[p2[i].index]=dist[u]+dist[v]-2*dist[ser(v)];        }        else        {            res[p2[i].index]=-1;        }    }    return ;}int ser(int x){    int l=x, r=x, j;    while(l!=par[l]) l=par[l];    while(r!=l) j=par[r], par[r]=l, r=j;    return l;}
1 0