HDU 5441

Problem Description
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,T5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n20000,m100000,q5000. The Undirected Kingdom has n cities and mbidirectional 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 d100000. 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
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
 

n个城市,m条路,每条路有一个权值w; q个询问每次输入一个数temp,这个人可以走任何权值<=temp的路,
问这个人能走多少对城市 (城市(a,b)和(b,a)算两对); 就是算这个图有多少连通分量(不符合<=temp的边不加入图中),
对于每个连通分量,例如某连通分量有ABC三个城市,则有AB BA AC CA BC CB六个城市对;即有n个城市的连通分量
有n(n-1)个城市对;每个连通分量的城市数就是该集合根节点的秩(代码中的num[]数组); 
计算结果时,每新加入一条边后的结果ans_now,可以由为加入该条边之前的结果ans递推得到;
例如两个连通分量的节点数为 fu ,fv;则join这两个连通分量后的结果ans_now=ans+(fu+fv)*(fu+fv-1)-fu*(fu-1)-fv*(fv-1)=2*fu*fv;


作者:WePlayDirty

  1 #include <iostream>
  2 #include <bits/stdc++.h>
  3 #define MAX 30005
  4 #define foi1(n) for(int i=0;i<n;i++)
  5 using namespace std;
  6 
  7 struct node
  8 {
  9     int u,v;
 10     int w;
 11 }st[1000005];
 12 
 13 struct nod
 14 {
 15     int val;
 16     int id;
 17     friend bool operator<(nod a,nod b)
 18     {
 19         return a.val<b.val;
 20     }
 21 }qu[5005];
 22 
 23 bool cmp(node a,node b)
 24 {
 25     return a.w<b.w;
 26 }
 27 int father[20005];
 28 int num[20005];
 29 void initial(int n)
 30 {
 31     int i;
 32     for(i=1;i<=n;++i)
 33     {
 34         father[i]=i;
 35         num[i]=1;
 36     }
 37 }
 38 int find(int x)
 39 {
 40     int t=x;
 41     while(x!=father[x])
 42         x=father[x];
 43     while(t!=father[t])
 44     {
 45         int temp=t;
 46         t=father[t];
 47         father[temp]=x;
 48     }
 49     return x;
 50 }
 51 void hy(int x,int y)
 52 {
 53     x=find(x);
 54     y=find(y);
 55     if(x!=y)
 56     {
 57         father[y]=x;
 58         num[x]+=num[y];
 59     }
 60 }
 61 int ans[6000];
 62 int main()
 63 {
 64     int T;
 65     scanf("%d",&T);
 66     while(T--)
 67     {
 68         int n,m,q;
 69         scanf("%d%d%d",&n,&m,&q);
 70         int i;
 71         for(i=1;i<=m;++i)
 72         {
 73             scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].w);
 74         }
 75         sort(st+1,st+m+1,cmp);
 76         for(i=1;i<=q;++i)
 77         {
 78             scanf("%d",&qu[i].val);
 79             qu[i].id=i;
 80         }
 81         sort(qu+1,qu+q+1);
 82         int t=1;
 83         initial(n);
 84         int s=0;
 85         for(i=1;i<=q;++i)
 86         {
 87             int temp;
 88             temp=qu[i].val;
 89             while(t<=m&&st[t].w<=temp)
 90             {
 91                 int u=st[t].u;
 92                 int v=st[t].v;
 93                 int x=find(u);
 94                 int y=find(v);
 95                 if(x!=y)
 96                 {
 97                     s+=2*num[x]*num[y];//叠加计算总的
 98                  hy(u,v);
 99                  /*我想的是用插入边之后的根节点对应的num来计算s=num*(num-1),一直WA,可能是因为在插边后根节点不止一个???*/
100                 }
101                 t++;
102             }
103             ans[qu[i].id]=s;
104         }
105         for(i=1;i<=q;++i)
106             printf("%d\n",ans[i]);
107     }
108     return 0;
109 }
View Code

 

转自
https://blog.csdn.net/u013569304/article/details/48475251

全部评论

相关推荐

05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务