HDU6166 Senior Pan 解题报告【图论】【SPFA最短路】【随机】
2017-08-25 17:31
405 查看
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
解题报告、
这道题的题意是,给我们一张图,问图上k个给定的点两辆最短路最小的值是多少。
我们假设答案的那个点中的一个在k个点中的前k/2个点中,后一个点在k个点中的后k/2个点中,那么我们建一个超级源点,一个超级汇点,把这k个点的一头一尾连起来,那么从超级源点出发跑到超级汇点跑一个最短路就行了。
为了保证答案的正确性,我们用随机函数打乱k个点的顺序,把上述过程重复很多遍(17-30遍)就行了。
代码如下:
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
解题报告、
这道题的题意是,给我们一张图,问图上k个给定的点两辆最短路最小的值是多少。
我们假设答案的那个点中的一个在k个点中的前k/2个点中,后一个点在k个点中的后k/2个点中,那么我们建一个超级源点,一个超级汇点,把这k个点的一头一尾连起来,那么从超级源点出发跑到超级汇点跑一个最短路就行了。
为了保证答案的正确性,我们用随机函数打乱k个点的顺序,把上述过程重复很多遍(17-30遍)就行了。
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define NAME "graph" using namespace std; const int N=100000,M=100000,inf=0x7fffffff; struct edge { int v,w,next; }ed[M+5]; int head[N+5],num; int dis[N+5],flag[N+5],p[N+5],x[N+5],y[N+5],w[N+5]; int n,m,k; int ans,T,kase; void build(int u,int v,int w) { ed[++num].v=v; ed[num].w=w; ed[num].next=head[u]; head[u]=num; } void SPFA(int st,int end) { for(int i=1;i<=n+2;i++)dis[i]=inf; memset(flag,0,sizeof(flag)); deque<int>q; q.push_back(st); flag[st]=1,dis[st]=0; while(!q.empty()) { int u=q.front();q.pop_front(); flag[u]=0; for(int i=head[u];i!=-1;i=ed[i].next) { int v=ed[i].v; if(dis[v]>dis[u]+ed[i].w) { dis[v]=dis[u]+ed[i].w; if(!flag[v]) { flag[v]=1; if(!q.empty()) { if(dis[v]>dis[q.front()])q.push_back(v); else q.push_front(v); } else q.push_back(v); } } } } ans=min(ans,dis[end]); } void solve() { ans=inf; int temp=30; while(temp--) { int st=n+1,end=n+2; num=0; memset(head,-1,sizeof(head)); random_shuffle(p+1,p+k+1); for(int i=1;i<m;i++)build(x[i],y[i],w[i]); for(int i=1;i<=k/2;i++)build(st,p[i],0); for(int i=k/2+1;i<=k;i++)build(p[i],end,0); SPFA(st,end); } } int main() { scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); num=0; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++)scanf("%d%d%d",&x[i],&y[i],&w[i]); for(int i=1;i<=k;i++)scanf("%d",&p[i]); solve(); printf("Case #%d: %d",++kase,ans); } return 0; }
相关文章推荐
- poj 3255 Roadblocks 次短路 spfa 解题报告
- BZOJ 4152 最短路(SPFA) 解题报告
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- luogu解题报告:P1119灾后重建【图论/最短路】
- POJ1062 昂贵的聘礼 解题报告【最短路/SPFA】
- POJ1724 ROADS 解题报告【最短路/SPFA】
- luogu解题报告:P1186玛丽卡【图论/最短路/堆优化dijkstra】
- hdu 2544 最短路 spfa 解题报告
- poj 2449 Remmarguts' Date 第k短路 A*+spfa 解题报告
- 洛谷 2737 麦香牛块 最短路SPFA? 解题报告
- BZOJ 2118 数论+最短路(SPFA) 解题报告
- CodeVS1077 多源最短路 解题报告【SPFA】
- 最短路spfaPOJInvitation Cardsj解题报告
- LuoguP1144 最短路计数 解题报告【SPFA/BFS】
- 图论专题测试 【最短路】【网络流】解题报告
- BZOJ 2079 [Poi 2010] 图论 解题报告
- hdu 3416 Marriage Match IV 【图论-网络流-最短路+最大流(spfa + Dinic)】
- HDU2544 最短路 解题报告--Dijkstra
- hdu 5418 Victor and World 最短路 floyd 解题报告
- 热浪解题报告(SPFA)