HDU 3938 Portal (離線並查集,此題思路很強!!!,得到所謂的距離很巧妙)
阿新 • • 發佈:2018-08-12
rain multipl rtu int total als arc sep 順序
Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
Output
Output the answer to each query on a separate line.
Recommend
We have carefully selected several similar problems for you: 3935 3937 3931 3932 3933
分析:
題目意思:
給你一個圖,帶權,問你兩點間的距離小於等於L的點對的數量 兩點間距離的定義:
兩點間所有路徑中,最長的邊中的最小的邊(很多路中的最長的哪些邊中的最小值) 註意理解 先將L升序排序,將邊按照輸入的權值升序排序
原因:比如L1<L2,現在得到L1的答案(兩點間的距離小於等於L的點對的數量),現在要知道L2的答案,L2的答案肯定是包含L1的答案的
是L1的答案加上某個值(該值必須是不與前面值有重疊的部分),所以我們先得到小一點的L的答案,然後通過小一點的L的答案求大一點的
L的答案,然後通過L輸入的順序按照順序輸出,這個就是所謂的離線化
然後開始從第一個最小的L開始跑,此時邊也是從最小權值的邊開始跑的,此代碼最神奇的地方在於得到兩點間所謂的距離:
原因:因為一開始是按照權值升序排序的,那麽此時得到的邊是集合中所有邊中最大的那個,也就是所有路中最長邊的最小值,因為你一開始兩個
點是沒有連通的,一旦通過此邊去連通,那麽此時能走的就只有這一條路,你肯定是要經過的,且此邊是目前所有邊中最長的那條邊,所以此邊就是
兩點間所謂的距離!!! 本人很菜,可能沒有說清楚,大家可以看看代碼,主要是for循環和內部的while循環部分 很難理解,也很奇妙 code:
Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2027 Accepted Submission(s): 998
Sample Input 10 10 10 7 2 1 6 8 3 4 5 8 5 8 2 2 8 9 6 4 5 2 1 5 8 10 5 7 3 7 7 8 8 10 6 1 5 9 1 8 2 7 6
Sample Output 36 13 1 13 36 1 36 2 16 13
Source 2011 Multi-University Training Contest 10 - Host by HRBEU
給你一個圖,帶權,問你兩點間的距離小於等於L的點對的數量 兩點間距離的定義:
兩點間所有路徑中,最長的邊中的最小的邊(很多路中的最長的哪些邊中的最小值) 註意理解 先將L升序排序,將邊按照輸入的權值升序排序
原因:比如L1<L2,現在得到L1的答案(兩點間的距離小於等於L的點對的數量),現在要知道L2的答案,L2的答案肯定是包含L1的答案的
是L1的答案加上某個值(該值必須是不與前面值有重疊的部分),所以我們先得到小一點的L的答案,然後通過小一點的L的答案求大一點的
L的答案,然後通過L輸入的順序按照順序輸出,這個就是所謂的離線化
然後開始從第一個最小的L開始跑,此時邊也是從最小權值的邊開始跑的,此代碼最神奇的地方在於得到兩點間所謂的距離:
原因:因為一開始是按照權值升序排序的,那麽此時得到的邊是集合中所有邊中最大的那個,也就是所有路中最長邊的最小值,因為你一開始兩個
點是沒有連通的,一旦通過此邊去連通,那麽此時能走的就只有這一條路,你肯定是要經過的,且此邊是目前所有邊中最長的那條邊,所以此邊就是
兩點間所謂的距離!!! 本人很菜,可能沒有說清楚,大家可以看看代碼,主要是for循環和內部的while循環部分 很難理解,也很奇妙 code:
#include<queue> #include<set> #include<cstdio> #include <iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; #define N 10005 #define M 50005 int pa[N]; int sum[N]; int n,m; struct node1 { int id,ans,l; }query[N]; struct node2 { int u,v,w; }edge[M]; bool cmp1(node2 a,node2 b) { return a.w<b.w; } bool cmp2(node1 a,node1 b) { return a.id<b.id; } bool cmp3(node1 a,node1 b) { return a.l<b.l; } void init() { for(int i=1;i<=n;i++) { pa[i]=i; sum[i]=1;//集合內點的數量 } } int find_set(int x) { if(x!=pa[x]) pa[x]=find_set(pa[x]); return pa[x]; } int union_set(int x,int y) { int fx=find_set(x); int fy=find_set(y); int temp=0; if(fx!=fy) { pa[fx]=fy; temp=sum[fx]*sum[fy];//沒有重疊部分的答案就是倆個集合點數量的乘積 sum[fy]+=sum[fx];//合並之後大集合點的數量等於兩個小集合點數量之和 } return temp;//當兩點是連通的時候,返回的是0,說明當前L得到的答案就是前面小一點的L的答案 } int main() { int q; while(~scanf("%d %d %d",&n,&m,&q)) { init(); for(int i=0;i<m;i++) { scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w); } for(int i=0;i<q;i++) { scanf("%d",&query[i].l); query[i].id=i;//按照L輸入順序輸出結果的保證 query[i].ans=0; } sort(edge,edge+m,cmp1);//邊按照權值排序 升序 sort(query,query+q,cmp3);//問詢按照L排序 升序 int cnt=0; for(int i=0;i<q;i++) { while(edge[cnt].w<=query[i].l&&cnt<m)//當前的W就是兩點間定義的距離 很神奇!!! { int x=edge[cnt].u; int y=edge[cnt].v; query[i].ans+=union_set(x,y); cnt++;//cnt一直在++,保證了當前大L算出來的值和前面小一點的L算出來的值是沒有重疊部分的 } if(i>0) query[i].ans+=query[i-1].ans;//此時大L的值是前面小L的值加上沒有重疊部分的值,前面while循環得到的值是沒有重疊部分的值 } sort(query,query+q,cmp2);//按照L的輸入順序輸出結果 for(int i=0;i<q;i++) { printf("%d\n",query[i].ans); } } return 0; } /* 題目意思: 給你一個圖,帶權,問你兩點間的距離小於等於L的點對的數量 兩點間距離的定義: 兩點間所有路徑中,最長的邊中的最小的邊(很多路中的最長的哪些邊中的最小值) 註意理解 先將L升序排序,將邊按照輸入的權值升序排序 原因:比如L1<L2,現在得到L1的答案(兩點間的距離小於等於L的點對的數量),現在要知道L2的答案,L2的答案肯定是包含L1的答案的 是L1的答案加上某個值(該值必須是不與前面值有重疊的部分),所以我們先得到小一點的L的答案,然後通過小一點的L的答案求大一點的 L的答案,然後通過L輸入的順序按照順序輸出,這個就是所謂的離線化 然後開始從第一個最小的L開始跑,此時邊也是從最小權值的邊開始跑的,此代碼最神奇的地方在於得到兩點間所謂的距離: 原因:因為一開始是按照權值升序排序的,那麽此時得到的邊是集合中所有邊中最大的那個,也就是所有路中最長邊的最小值,因為你一開始兩個 點是沒有連通的,一旦通過此邊去連通,那麽此時能走的就只有這一條路,你肯定是要經過的,且此邊是目前所有邊中最長的那條邊,所以此邊就是 兩點間所謂的距離!!! */
HDU 3938 Portal (離線並查集,此題思路很強!!!,得到所謂的距離很巧妙)