hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)
2017-10-06 20:43
435 查看
Senior Pan
Problem DescriptionSenior 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.
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 integers xi,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 integers ai, the nodes that Master Dong selects out.1≤ai≤n,ai!=aj
Output
For every Test Case, output one integer: the answer
Sample Input
1
5 6
1 2 1
2 3 3
3 1 3
2 5 1
2 4 2
4 3 1
3
1 3 5
Sample Output
Case #1: 2
思路:要求k个点中两点的最小值,肯定不能直接遍历,那么就考虑分类了
枚举二进制的每一位,对这k个点进行分类,若第i位为1则分到s1集合中,否则分到集合s2中,同理反着在再来一次(有向图嘛)
这样就保证了枚举所有的情况(假设点s到点t的距离最短,点s肯定不等于点t,也就是说点s和点t在二进制上至少有一位不同)
代码:
#include<bits/stdc++.h> using namespace std; typedef long long LL; const LL inf=0x3f3f3f3f3f3f3f3f; const int maxn=1e5+10; struct node { 4000 int u,v; LL w; } p[maxn]; struct edge { int v,next; LL w; } E[maxn<<1]; int first[maxn],num[maxn]; LL dis[maxn]; bool inq[maxn]; int n,m,k,len; LL Min_ans; void init() { len=0; memset(first,-1,sizeof(first)); memset(dis,inf,sizeof(dis)); memset(inq,false,sizeof(inq)); } void add_edge(int u,int v,LL w) { E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++; } void Build_edge() { init(); for(int j=0; j<m; ++j) add_edge(p[j].u,p[j].v,p[j].w); } LL spfa(int st,int ed) { queue<int>q; q.push(st),inq[st]=true,dis[st]=0; while(!q.empty()) { st=q.front(); inq[st]=false,q.pop(); for(int i=first[st]; ~i; i=E[i].next) { int v=E[i].v; LL w=E[i].w; if(dis[v]>dis[st]+w) { dis[v]=dis[st]+w; if(!inq[v]) { inq[v]=true; q.push(v); } } } } return dis[ed]; } int main() { int t,Case=0; scanf("%d",&t); while(++Case<=t) { Min_ans=inf; scanf("%d%d",&n,&m); for(int i=0; i<m; ++i) scanf("%d%d%lld",&p[i].u,&p[i].v,&p[i].w); scanf("%d",&k); for(int i=1; i<=k; ++i) scanf("%d",&num[i]); int res=log(n*1.0)/log(2.0); int cnt=res==(int)res?res:res+1; for(int i=0; i<=res; ++i) { int tot=1<<i; Build_edge(); for(int j=1; j<=k; ++j) { if(num[j]&tot) add_edge(0,num[j],0); else add_edge(num[j],n+1,0); } Min_ans=min(Min_ans,spfa(0,n+1)); Build_edge(); for(int j=1; j<=k; ++j) { if(num[j]&tot) add_edge(num[j],n+1,0); else add_edge(0,num[j],0); } Min_ans=min(Min_ans,spfa(0,n+1)); } printf("Case #%d: %lld\n",Case,Min_ans); } return 0; }
相关文章推荐
- 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(多源最短路径)(二进制划分集合)
- hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)
- hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)
- hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)
- HDU 6166 Senior Pan (多源多汇最短路+集合的二进制划分 17多校第九场第6题)
- HDU 6166 Senior Pan(二进制划分集合)
- HDU6166Senior Pan(顶点子集最短路径-二进制划分集合)
- 文章标题 HDU 6166 : Senior Pan(二进制+dijkstra)
- HDU 6166 Senior Pan(多校第九场 二进制分组最短路)
- HDU 6166 Senior Pan 【二进制分组最短路 】 好题