HDU 3938 Portal 锛堢绾垮苟鏌ラ泦锛屾棰樻€濊矾寰堝己锛侊紒锛侊紝寰楀埌鎵€璋撶殑璺濈寰堝阀濡欙級
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3938 Portal 锛堢绾垮苟鏌ラ泦锛屾棰樻€濊矾寰堝己锛侊紒锛侊紝寰楀埌鎵€璋撶殑璺濈寰堝阀濡欙級相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/div' title='div'>div
lan 椤哄簭 鍒嗘瀽 using find 璺濈 other necPortal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2027 Accepted Submission(s): 998
Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
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.
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
Recommend
鍒嗘瀽锛?/span>
棰樼洰鎰忔€濓細
缁欎綘涓€涓浘锛屽甫鏉冿紝闂綘涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺
缁欎綘涓€涓浘锛屽甫鏉冿紝闂綘涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺
涓ょ偣闂磋窛绂荤殑瀹氫箟锛?/span>
涓ょ偣闂存墍鏈夎矾寰勪腑锛屾渶闀跨殑杈逛腑鐨勬渶灏忕殑杈癸紙寰堝璺腑鐨勬渶闀跨殑鍝簺杈逛腑鐨勬渶灏忓€硷級 娉ㄦ剰鐞嗚В
涓ょ偣闂存墍鏈夎矾寰勪腑锛屾渶闀跨殑杈逛腑鐨勬渶灏忕殑杈癸紙寰堝璺腑鐨勬渶闀跨殑鍝簺杈逛腑鐨勬渶灏忓€硷級 娉ㄦ剰鐞嗚В
鍏堝皢L鍗囧簭鎺掑簭锛屽皢杈规寜鐓ц緭鍏ョ殑鏉冨€煎崌搴忔帓搴?/span>
鍘熷洜锛氭瘮濡侺1<L2,鐜板湪寰楀埌L1鐨勭瓟妗堬紙涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺锛夛紝鐜板湪瑕佺煡閬揕2鐨勭瓟妗堬紝L2鐨勭瓟妗堣偗瀹氭槸鍖呭惈L1鐨勭瓟妗堢殑
鏄疞1鐨勭瓟妗堝姞涓婃煇涓€硷紙璇ュ€煎繀椤绘槸涓嶄笌鍓嶉潰鍊兼湁閲嶅彔鐨勯儴鍒嗭級锛屾墍浠ユ垜浠厛寰楀埌灏忎竴鐐圭殑L鐨勭瓟妗堬紝鐒跺悗閫氳繃灏忎竴鐐圭殑L鐨勭瓟妗堟眰澶т竴鐐圭殑
L鐨勭瓟妗堬紝鐒跺悗閫氳繃L杈撳叆鐨勯『搴忔寜鐓ч『搴忚緭鍑猴紝杩欎釜灏辨槸鎵€璋撶殑绂荤嚎鍖?/span>
鍘熷洜锛氭瘮濡侺1<L2,鐜板湪寰楀埌L1鐨勭瓟妗堬紙涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺锛夛紝鐜板湪瑕佺煡閬揕2鐨勭瓟妗堬紝L2鐨勭瓟妗堣偗瀹氭槸鍖呭惈L1鐨勭瓟妗堢殑
鏄疞1鐨勭瓟妗堝姞涓婃煇涓€硷紙璇ュ€煎繀椤绘槸涓嶄笌鍓嶉潰鍊兼湁閲嶅彔鐨勯儴鍒嗭級锛屾墍浠ユ垜浠厛寰楀埌灏忎竴鐐圭殑L鐨勭瓟妗堬紝鐒跺悗閫氳繃灏忎竴鐐圭殑L鐨勭瓟妗堟眰澶т竴鐐圭殑
L鐨勭瓟妗堬紝鐒跺悗閫氳繃L杈撳叆鐨勯『搴忔寜鐓ч『搴忚緭鍑猴紝杩欎釜灏辨槸鎵€璋撶殑绂荤嚎鍖?/span>
鐒跺悗寮€濮嬩粠绗竴涓渶灏忕殑L寮€濮嬭窇锛屾鏃惰竟涔熸槸浠庢渶灏忔潈鍊肩殑杈瑰紑濮嬭窇鐨勶紝姝や唬鐮佹渶绁炲鐨勫湴鏂瑰湪浜庡緱鍒颁袱鐐归棿鎵€璋撶殑璺濈锛?/span>
鍘熷洜锛氬洜涓轰竴寮€濮嬫槸鎸夌収鏉冨€煎崌搴忔帓搴忕殑锛岄偅涔堟鏃跺緱鍒扮殑杈规槸闆嗗悎涓墍鏈夎竟涓渶澶х殑閭d釜锛屼篃灏辨槸鎵€鏈夎矾涓渶闀胯竟鐨勬渶灏忓€硷紝鍥犱负浣犱竴寮€濮嬩袱涓?/span>
鐐规槸娌℃湁杩為€氱殑锛屼竴鏃﹂€氳繃姝よ竟鍘昏繛閫氾紝閭d箞姝ゆ椂鑳借蛋鐨勫氨鍙湁杩欎竴鏉¤矾锛屼綘鑲畾鏄缁忚繃鐨勶紝涓旀杈规槸鐩墠鎵€鏈夎竟涓渶闀跨殑閭f潯杈癸紝鎵€浠ユ杈瑰氨鏄?/span>
涓ょ偣闂存墍璋撶殑璺濈锛侊紒锛?/span>
鏈汉寰堣彍锛屽彲鑳芥病鏈夎娓呮锛屽ぇ瀹跺彲浠ョ湅鐪嬩唬鐮侊紝涓昏鏄痜or寰幆鍜屽唴閮ㄧ殑while寰幆閮ㄥ垎
寰堥毦鐞嗚В锛屼篃寰堝濡?/span>
code锛?/span>
#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;//闆嗗悎鍐呯偣鐨勬暟閲?/span> } } 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];//娌℃湁閲嶅彔閮ㄥ垎鐨勭瓟妗堝氨鏄咯涓泦鍚堢偣鏁伴噺鐨勪箻绉?/span> sum[fy]+=sum[fx];//鍚堝苟涔嬪悗澶ч泦鍚堢偣鐨勬暟閲忕瓑浜庝袱涓皬闆嗗悎鐐规暟閲忎箣鍜?/span> } return temp;//褰撲袱鐐规槸杩為€氱殑鏃跺€欙紝杩斿洖鐨勬槸0锛岃鏄庡綋鍓峀寰楀埌鐨勭瓟妗堝氨鏄墠闈㈠皬涓€鐐圭殑L鐨勭瓟妗?/span> } 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杈撳叆椤哄簭杈撳嚭缁撴灉鐨勪繚璇?/span> 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)//褰撳墠鐨刉灏辨槸涓ょ偣闂村畾涔夌殑璺濈 寰堢濂囷紒锛侊紒 { int x=edge[cnt].u; int y=edge[cnt].v; query[i].ans+=union_set(x,y); cnt++;//cnt涓€鐩村湪++锛屼繚璇佷簡褰撳墠澶绠楀嚭鏉ョ殑鍊煎拰鍓嶉潰灏忎竴鐐圭殑L绠楀嚭鏉ョ殑鍊兼槸娌℃湁閲嶅彔閮ㄥ垎鐨?/span> } if(i>0) query[i].ans+=query[i-1].ans;//姝ゆ椂澶鐨勫€兼槸鍓嶉潰灏廘鐨勫€煎姞涓婃病鏈夐噸鍙犻儴鍒嗙殑鍊硷紝鍓嶉潰while寰幆寰楀埌鐨勫€兼槸娌℃湁閲嶅彔閮ㄥ垎鐨勫€?/span> } sort(query,query+q,cmp2);//鎸夌収L鐨勮緭鍏ラ『搴忚緭鍑虹粨鏋?/span> for(int i=0;i<q;i++) { printf("%d ",query[i].ans); } } return 0; } /* 棰樼洰鎰忔€濓細 缁欎綘涓€涓浘锛屽甫鏉冿紝闂綘涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺 涓ょ偣闂磋窛绂荤殑瀹氫箟锛? 涓ょ偣闂存墍鏈夎矾寰勪腑锛屾渶闀跨殑杈逛腑鐨勬渶灏忕殑杈癸紙寰堝璺腑鐨勬渶闀跨殑鍝簺杈逛腑鐨勬渶灏忓€硷級 娉ㄦ剰鐞嗚В 鍏堝皢L鍗囧簭鎺掑簭锛屽皢杈规寜鐓ц緭鍏ョ殑鏉冨€煎崌搴忔帓搴? 鍘熷洜锛氭瘮濡侺1<L2,鐜板湪寰楀埌L1鐨勭瓟妗堬紙涓ょ偣闂寸殑璺濈灏忎簬绛変簬L鐨勭偣瀵圭殑鏁伴噺锛夛紝鐜板湪瑕佺煡閬揕2鐨勭瓟妗堬紝L2鐨勭瓟妗堣偗瀹氭槸鍖呭惈L1鐨勭瓟妗堢殑 鏄疞1鐨勭瓟妗堝姞涓婃煇涓€硷紙璇ュ€煎繀椤绘槸涓嶄笌鍓嶉潰鍊兼湁閲嶅彔鐨勯儴鍒嗭級锛屾墍浠ユ垜浠厛寰楀埌灏忎竴鐐圭殑L鐨勭瓟妗堬紝鐒跺悗閫氳繃灏忎竴鐐圭殑L鐨勭瓟妗堟眰澶т竴鐐圭殑 L鐨勭瓟妗堬紝鐒跺悗閫氳繃L杈撳叆鐨勯『搴忔寜鐓ч『搴忚緭鍑猴紝杩欎釜灏辨槸鎵€璋撶殑绂荤嚎鍖? 鐒跺悗寮€濮嬩粠绗竴涓渶灏忕殑L寮€濮嬭窇锛屾鏃惰竟涔熸槸浠庢渶灏忔潈鍊肩殑杈瑰紑濮嬭窇鐨勶紝姝や唬鐮佹渶绁炲鐨勫湴鏂瑰湪浜庡緱鍒颁袱鐐归棿鎵€璋撶殑璺濈锛? 鍘熷洜锛氬洜涓轰竴寮€濮嬫槸鎸夌収鏉冨€煎崌搴忔帓搴忕殑锛岄偅涔堟鏃跺緱鍒扮殑杈规槸闆嗗悎涓墍鏈夎竟涓渶澶х殑閭d釜锛屼篃灏辨槸鎵€鏈夎矾涓渶闀胯竟鐨勬渶灏忓€硷紝鍥犱负浣犱竴寮€濮嬩袱涓? 鐐规槸娌℃湁杩為€氱殑锛屼竴鏃﹂€氳繃姝よ竟鍘昏繛閫氾紝閭d箞姝ゆ椂鑳借蛋鐨勫氨鍙湁杩欎竴鏉¤矾锛屼綘鑲畾鏄缁忚繃鐨勶紝涓旀杈规槸鐩墠鎵€鏈夎竟涓渶闀跨殑閭f潯杈癸紝鎵€浠ユ杈瑰氨鏄? 涓ょ偣闂存墍璋撶殑璺濈锛侊紒锛? */
以上是关于HDU 3938 Portal 锛堢绾垮苟鏌ラ泦锛屾棰樻€濊矾寰堝己锛侊紒锛侊紝寰楀埌鎵€璋撶殑璺濈寰堝阀濡欙級的主要内容,如果未能解决你的问题,请参考以下文章