floyd+并查集缩点

来源:互联网 发布:淘宝跳转链接 编辑:程序博客网 时间:2024/05/22 06:41


Description

 There are countries at planet X on which Xiao Ming was born.

 

Some countries, which have been sharing fine bilateral relations, form a coalition and thus all of their citizens will benefit from a policy for which all the travels between these countries will become totally free.

 

But it is not easy to travel between countries distributed in different coalitions. If possible, it must cost some money called YZB(yu zhou bi) which is always positive.

 

Help Xiao Ming determine the minimum cost between countries.

Input
The input consists of one or more test cases.

First line of each test case consists two integers n and m. (1<=n<=10^5, 1<=m<=10^5)

Each of the following m lines contains: x y c, and c indicating the YZB traveling from x to y or from y to x. If it equals to zero, that means x and y are in the same coalition. (1<=x, y<=n, 0<=c<=10^9)
You can assume that there are no more than one road between two countries.

Then the next line contains an integer q, the number of queries.(1<=q<=200)

Each of the following q lines contains: x y. (1<=x, y<=n)

It is guaranteed that there are no more 200 coalitions.

Input is terminated by a value of zero (0) for n.
Output
For each test case, output each query in a line. If it is impossible, output “-1”.
 
Sample Input
6 5
1 2 0
2 4 1
3 5 0
1 4 1
1 6 2
3
4 2
1 3
4 6
0
Sample Output
1
-1
3

并查集对同盟的国家缩点,然后重新编号,因为保证同盟数再200以内,然后就是Floyd求多源最短路
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100010;const int INF=1000500000;int pre[maxn],rank[maxn],idx[maxn];int grid[300][300];int u[maxn],v[maxn],w[maxn];int n,m,num,q;void init(){    for(int i=0;i<=n;i++)    {        pre[i]=i;        rank[i]=0;    }    memset(idx,-1,sizeof(idx));    for(int i=0;i<=200;i++)        for(int j=0;j<=200;j++)grid[i][j]=INF;    num=1;}int find(int x){    if(pre[x]==x)return x;    return pre[x]=find(pre[x]);}void unite(int u,int v){    int x=find(u);    int y=find(v);    if(x==y)return ;    if(rank[x]<rank[y])pre[x]=y;    else    {        pre[y]=x;        if(rank[x]==rank[y])rank[x]++;    }}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF,n)    {        scanf("%d",&m);        init();        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",u+i,v+i,w+i);            if(!w[i])unite(u[i],v[i]);        }        for(int i=1;i<=m;i++)        {            int x=find(u[i]);            int y=find(v[i]);            if(idx[x]<0)idx[x]=num++;            if(idx[y]<0)idx[y]=num++;            grid[idx[x]][idx[y]]=grid[idx[y]][idx[x]]=min(grid[idx[x]][idx[y]],w[i]);        }        for(int k=1;k<num;k++)        for(int i=1;i<num;i++)        {            for(int j=1;j<num;j++)            {                if(i==j)continue;                grid[i][j]=min(grid[i][j],grid[i][k]+grid[k][j]);            }        }        scanf("%d",&q);        int q1,q2;        while(q--)        {            scanf("%d%d",&q1,&q2);            int x=find(q1);            int y=find(q2);            if(x==y)printf("0\n");            else if(grid[idx[x]][idx[y]]>=INF)printf("-1\n");            else printf("%d\n",grid[idx[x]][idx[y]]);        }    }    return 0;}


0 0
原创粉丝点击