【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra
来源:互联网 发布:卖汉服的淘宝店 编辑:程序博客网 时间:2024/06/04 22:48
Problem Description
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday.
The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.
The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.
Input
The first line contains one integer T, represents the number of Test Cases.1≤T≤5.Then T Test Cases, for each Test Cases, the first line contains two integers n,m representing the number of nodes and the number of edges.1≤n,m≤100000
Then m lines follow. Each line contains three integersxi,yi representing an edge, and vi representing its length.1≤xi,yi ≤n,1≤vi ≤100000
Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n
The following line contains K unique integersai , the nodes that Master Dong selects out.1≤ai ≤n,ai !=aj
Then m lines follow. Each line contains three integers
Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n
The following line contains K unique integers
Output
For every Test Case, output one integer: the answer
Sample Input
15 61 2 12 3 33 1 32 5 12 4 24 3 131 3 5
Sample Output
Case #1: 2
题意:
一个有向图,选其中k个点,求两点间距离的最小值。
思路:
我们只要求出任意点对的最小值即可,但是暴力做肯定会超时。把k个点分成两个集合,用dijkstra求两个集合间的最小值==求两个集合间的所有点对的最小值。
由于任意两点的二进制不同,我们可以枚举二进制的位,将k分成两个集合。只需要logn次dijkstra就能得到所有点对间的最小值。
//// main.cpp// 1006//// Created by zc on 2017/8/23.// Copyright © 2017年 zc. All rights reserved.//#include <iostream>#include<cstdio>#include<queue>#include<cstring>#include<cmath>#include<algorithm>#define ll long longusing namespace std;const int M=440000;const int N=110000;const ll INF=0x3f3f3f3f3f3f3f3f;int n,m,cnt,head[N],k,a[N],v[N];ll d[N];struct node{ int next,v; ll c;}e[M];struct node2{ int u; ll c; friend bool operator< (node2 a ,node2 b) { return a.c>b.c; }};priority_queue<node2> q;void add(int x,int y,ll c){ e[cnt].c=c; e[cnt].v=y; e[cnt].next=head[x]; head[x]=cnt++;}void init(){ memset(v,0,sizeof(v)); memset(d,0x3f,sizeof(d)); while(!q.empty()) q.pop();}ll dijkstra(){ while(!q.empty()) { node2 t=q.top(); q.pop(); int u=t.u; ll c=t.c; if(v[u]) return c; for(int i=head[u];i+1;i=e[i].next) { int y=e[i].v; if(d[y]>d[u]+e[i].c) { d[y]=c+e[i].c; q.push(node2{y,d[y]}); } } } return INF;}ll solve(){ ll ans=INF; for(int i=0;i<20;i++) { init(); for(int j=0;j<k;j++) { if(a[j]&(1<<i)) { q.push(node2{a[j],0}); d[a[j]]=0; } else { v[a[j]]=1; } } ans=min(ans,dijkstra()); } return ans;}int main(int argc, const char * argv[]) { int T,kase=0; scanf("%d",&T); while(T--) { cnt=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int x,y; ll c; scanf("%d%d%lld",&x,&y,&c); add(x,y,c); } scanf("%d",&k); for(int i=0;i<k;i++) scanf("%d",&a[i]); printf("Case #%d: %lld\n",++kase,solve()); }}
阅读全文
0 0
- 【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra
- HDU 6166 && 2017 多校训练:Senior Pan(最短路)
- HDU-2017 多校训练赛9-1006-Senior Pan
- 【多校训练】hdu 6181 Two Paths 次短路径 Dijkstra
- hdu 6166 Senior Pan(巧妙的Dijkstra)
- [hdu 6166 Senior Pan]Dijkstra+概率随机
- 【多校训练】hdu 6071 Lazy Running 最短路径 spfa
- hdu 6166 Senior Pan 最短路
- hdu 6166 Senior Pan(最短路)
- HDU 6166 Senior Pan
- HDU 6166 Senior Pan
- [HDU 6166] Senior Pan
- HDU 6166 Senior Pan
- hdu-6166 Senior Pan
- HDU 6166 Senior Pan
- HDU 6166 Senior Pan
- HDU-6166 Senior Pan
- 文章标题 HDU 6166 : Senior Pan(二进制+dijkstra)
- mac上的几个键⌘——Command ⌃ ——Control是向上的尖括号⌃ ——option Z字加一杠⇧——Shift
- 多个按键点击映射到同一个消息函数中的方法(ON_COMMAND_RANGE)
- 深度解析链表操作
- HDU-5072 Coprime(容斥原理,同色三角形)
- Java开发中的23种设计模式详解(转)
- 【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra
- codeforces738C
- As Easy As Possible 倍增法
- 关系的三类完整性约束
- JavaWeb开发之Maven工程(SSM框架)
- Python类实现动物种类的派生
- TFileStream用法
- C++内存管理之结构体内存释放
- JFinal解决IE浏览器下载问题