Travel HDU

来源:互联网 发布:福建网络安全教育 编辑:程序博客网 时间:2024/06/08 19:15

Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and mm bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is xx minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
Input
The first line contains one integer T,T5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and qq where n20000,m100000,q5000. The Undirected Kingdom has n cities and m bidirectional roads, and there areq queries.

Each of the following mm lines consists of three integers a,b and d where a,b{1,...,n} and d100000. It takes Jack dd minutes to travel from city a to city b and vice versa.

Then qq lines follow. Each of them is a query consisting of an integer xx where x is the time limit before Jack goes berserk.

Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities (a,b) which Jack may travel from a to b within the time limit x.

Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
Sample Input
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
Sample Output
2
6
12
这题我没读懂题意,一直认为在所有一个点到另一个点的用时必须在规定的询问的时间之内,所有一直看不懂样例。
那么在给定时间,则只有小于这个时间的边才是有效边,然后建图,找连通分量的大小即C2n,这个就用带权,并查集实现就好了,现在问题是有多个询问,如果每个询问就重新做一次并查集,那效率太低了,把询问从小到大排序,现在还是面临一个问题,即使如此,如果每次询问以后,都要去线性去扫一遍有多少个连通分量和大小,效率也不高,后来想了挺久,每次询问和询问之间的值是有联系的,就是说可以转移的,那么每次加入边的时候,我们可以把上一次的询问值转移到这次询问值,这样的确高效一些,叫队友一直看这个题就是不看,说了有点看不懂,就是不看,真的烦,早点看,我就A了,感觉现在就是单人模式

#include<cstdio>#include<algorithm>#include<cstring>#define N 2005#define INF 0x3f3f3f3ftypedef long long ll;using namespace std;int n,m,q;struct node{    int x1,x2;    int w;}edge[100005];int cmp1(node p1,node p2){    return p1.w<p2.w;}int num[20005];int pre[20005];struct node2{    int val;    int index;}query[5005];int cmp2(node2 p1,node2 p2){    return p1.val<p2.val;}long long ans[5005];void init(){    for(int i=1;i<=n;i++)    {        pre[i]=i;        num[i]=1;    }}int findd(int x){    return pre[x]==x?x:pre[x]=findd(pre[x]);}long long get(int x){    return x*x-x;}int main(){    int t;    int x,y,w;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&q);        init();        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&w);            edge[i].x1=x;            edge[i].x2=y;            edge[i].w=w;        }        sort(edge,edge+m,cmp1);        for(int i=0;i<q;i++)        {            scanf("%d",&w);            query[i].val=w;            query[i].index=i;        }        sort(query,query+q,cmp2);        long long temp=0;        for(int i=0,j=0;i<q;i++)        {            while(j<m&&(edge[j].w<=query[i].val))            {                int xx=findd(edge[j].x1);                int yy=findd(edge[j].x2);                if(xx==yy)                {                    j++;                    continue;                }                if(num[xx]==1)                {                    if(num[yy]==1)                    {                        pre[xx]=yy;                        num[yy]++;                        temp+=get(2);                    }                    else                    {                        temp-=get(num[yy]);                        pre[xx]=yy;                        num[yy]++;                        temp+=get(num[yy]);                    }                }                else                {                    if(num[yy]==1)                    {                        temp-=get(num[xx]);                        pre[yy]=xx;                        num[xx]++;                        temp+=get(num[xx]);                    }                    else                    {                        temp-=get(num[xx]);                        temp-=get(num[yy]);                        pre[xx]=yy;                        num[yy]+=num[xx];                        temp+=get(num[yy]);                    }                }                j++;            }            ans[query[i].index]=temp;        }        for(int i=0;i<q;i++)            printf("%lld\n",ans[i]);    }    return 0;}
原创粉丝点击