Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1225 Accepted Submission(s): 443
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 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 b without going berserk?
Input
The first line contains one integer x is the time limit before Jack goes berserk.
Output
You should print 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
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int maxn=20010; const int maxm=100010; const int maxq=5005; struct node1{ int u,v,w; node1(){} node1(int u,int v,int w):u(u),v(v),w(w) {} }g[maxm]; struct node2{ int d,id; node2(){} node2(int d,int id):d(d),id(id) {} }que[5005]; long long ans[5005]; int num[maxn], rak[maxn],father[maxn]; bool cmp1(struct node1 t1,struct node1 t2){ return t1.w<t2.w; } bool cmp2(struct node2 t1,struct node2 t2){ return t1.d<t2.d; } int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x]; } long long tnum; void Union(int u,int v){ int x=find(u); int y=find(v); if(x==y) return ; tnum+=num[x]*num[y]; if(rak[x]<rak[y]){ father[x]=y; num[y]+=num[x]; num[x]=0; } else { father[y]=x; if(rak[x]==rak[y]) ++rak[x]; num[x]+=num[y]; num[y]=0; } } int main(){ int t; scanf("%d",&t); while(t--){ int n,m,q; scanf("%d%d%d",&n,&m,&q); int u,v,w; for(int i=1;i<=n;i++){ father[i]=i; num[i]=1; rak[i]=0; } for(int i=0;i<m;i++){ scanf("%d%d%d",&u,&v,&w); g[i]=node1(u,v,w); } sort(g,g+m,cmp1); int d; for(int i=0;i<q;i++){ scanf("%d",&d); que[i]=node2(d,i); } sort(que,que+q,cmp2); memset(ans,0,sizeof(ans)); tnum=0; for(int i=0,j=0;i<q;i++){ int cur=que[i].d; while(j<m){ node1 temp=g[j]; if(cur>=temp.w){ Union(temp.u,temp.v); } else break; j++; } ans[que[i].id]=tnum; } for(int i=0;i<q;i++) printf("%lld\n",ans[i]*2); } return 0; }