Hdu_6166多源多汇最短路

Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 424    Accepted Submission(s): 158


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.
 

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
 

Source

比赛的时候没想到这个方法。。因为要从k个点中选取路径最短的两个点,然后可以想到如果我们可以把每两个点当作起点和终点那么我们跑一下最短路就可以知道他们的路径长度了。因此,我们建立一个超级源点和超级汇点,(网络流中的思想吧),然后每次通过对k个点的二进制位进行划分,比如在某一位上都为1的点作为起点,为0的点做为终点,又因为是有向图所以还需要交换一下起点和终点。然后我们跑dijkstra堆优化的算法,就可以得到从超级源点到超级汇点的距离了,这个距离也是选取的k个点中两个距离最近的点,符合要求。因为做多10w个点大概2^20即跑20*2*nlgn的时间复杂度即可 进行一些细节优化可4秒内跑出。
	#include<bits/stdc++.h>
	using namespace std;
	const int maxn = 1e6+10;
	#define ll long long 
	const int INF = 1<<29;
	struct Node{
		int v;ll dis;
		Node(){}
		Node(ll dis,int v):dis(dis),v(v){}
	}node[maxn];
	struct Edge{
		int v,next,val;
	}edge[maxn*2];
	int head[maxn],cnt,a[maxn];
	int x[maxn],y[maxn],val[maxn];
	int n,m;
	ll dis[maxn];
	struct cmp{
		bool operator()(Node a, Node b){
			return a.dis>b.dis;
		}
	};
	void addedge(int from,int to,int val){
		edge[cnt].v=to;
		edge[cnt].next=head[from];
		edge[cnt].val=val;
		head[from]=cnt++;
	}
	void init(){
		memset(head,-1,sizeof(head));
		memset(edge,0,sizeof(edge));
		cnt=0;
	}
	ll dijkstra(){
		priority_queue<Node,vector<Node>,cmp >q;
		bool vis[maxn];
		for(int i=1;i<=n+1;i++){
			dis[i]=INF;
			vis[i]=false;
		}
		dis[0]=0ll;
		q.push(Node(dis[0],0));//入队 
		vis[0]=true;
		while(!q.empty()){
			Node temp = q.top();
			q.pop();
			int u = temp.v;
			for(int i=head[u];i!=-1;i=edge[i].next){
				int to = edge[i].v;
				if(vis[to])continue;
				vis[to]=false;
				if(dis[to]>dis[u]+(ll)edge[i].val){
					dis[to]=dis[u]+(ll)edge[i].val;
					q.push(Node(dis[to],to));
				}
			}
		}
		return dis[n+1];
	}
	int main(){
		int t;
		scanf("%d",&t);
		int ca=1;
		while(t--){
			scanf("%d%d",&n,&m);
			for(int i=1;i<=m;i++)scanf("%d%d%d",x+i,y+i,val+i);
			int k;scanf("%d",&k);
			int ma=-1;
			for(int i=1;i<=k;i++){
				scanf("%d",&a[i]);
				ma = max(a[i],ma);
			}
			int tt=0;
			while(ma){
				ma>>=1;
				tt++;
			} 
			ll ans=INF;
			for(int t=0;t<tt;t++){//选取大概做几次 
	//			init();
				cnt = 0;
				memset(head,-1,sizeof(head));
				for(int i=1;i<=m;i++){
					addedge(x[i],y[i],val[i]);
				}
				for(int i=1;i<=k;i++){
					if(a[i]&(1<<t)){
						addedge(0,a[i],0);
					}
					else{
						addedge(a[i],n+1,0);
					}
				}
				ans  = min(ans,dijkstra());	
					
				cnt = 0;
				memset(head,-1,sizeof(head));			
				for(int i=1;i<=m;i++){
					addedge(x[i],y[i],val[i]);
				}
				for(int i=1;i<=k;i++){
					if((a[i]&(1<<t))==0){
						addedge(0,a[i],0);
					}
					else{
						addedge(a[i],n+1,0);
					}
				}
				ans  = min(ans,dijkstra());		
			}
			printf("Case #%d: %lld\n",ca++,ans);
		}
	} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值