Travel HDU

来源:互联网 发布:剑三冷道姑艳捏脸数据 编辑:程序博客网 时间:2024/06/08 16:01
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 m 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 x 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,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and m bidirectional roads, and there are q queries.

Each of the following m lines consists of three integers a,b and d where a,b∈{1,…,n} and d≤100000. It takes Jack d minutes to travel from city a to city b and vice versa.

Then q lines follow. Each of them is a query consisting of an integer x 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

15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000

Sample Output

2612

太菜了 太菜了 因为输出顺序的问题浪费了3小时。
题意 n个点 m条边 每次询问给你一个最大长度len,问有多少对点(a->b)的单路线长度小于len。
完成输入后把所有询问存起来 从小到大排个序,再把所有边按长度排个序。
按询问顺序依次二分找出能加入并查集的边有哪些,然后合并这些点。
假设区块一有x个点 区块二有y个点 那么这次合并所增加的点就是2*x*y。
列个式子就出来了。

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<set>using namespace std;class node{public:    int u,v,cost;    node(int u,int v,int cost):u(u),v(v),cost(cost){}    node(){}};const int MAX=1e5+5;int par[MAX];int ranks[MAX];int nodesize[MAX];void init(int n){    for(int i=0;i<=n;i++)    {        par[i]=i;        ranks[i]=0;        nodesize[i]=1;    }}int finds(int x){    if(par[x]==x)    {        return x;    }    else{        return par[x]=finds(par[x]);    }}void unite(int x,int y){    x=finds(x);    y=finds(y);    if(x>y)        swap(x,y);    if(x==y) return ;    par[y]=x;    nodesize[x]+=nodesize[y];}bool cmp(node & a,node &b){    return a.cost<b.cost;}node nodes[MAX];node quest[MAX];int tcost[MAX];int ans[MAX];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m,q;        scanf("%d %d %d",&n,&m,&q);        for(int i=0;i<m;i++)        {            scanf("%d %d %d",&nodes[i].u,&nodes[i].v,&nodes[i].cost);            tcost[i]=nodes[i].cost;        }        sort(nodes,nodes+m,cmp);        //sort(tcost,tcost+m);        for(int i=0;i<q;i++)        {            scanf("%d",&quest[i].cost);            quest[i].v=i;        }        sort(quest,quest+q,cmp);        init(n+2);        vector<int > V;        int sum=0;        int star=0;        for(int i=0;i<q;i++)        {            while(star<m&&nodes[star].cost<=quest[i].cost)            {                int par1=finds(nodes[star].u);                int par2=finds(nodes[star].v);                star++;                if(par1==par2)                    continue;                sum+=nodesize[par1]*nodesize[par2]*2;                unite(par1,par2);            }            ans[quest[i].v]=sum;        }        for(int i=0;i<q;i++)        {            cout<<ans[i]<<endl;        }    }}
原创粉丝点击