首页 > 其他 > 详细

HDU 2680 Choose the best route

时间:2014-03-24 12:26:45      阅读:425      评论:0      收藏:0      [点我收藏+]

题解:由于是多个起点和单个终点,所以反向构图,那么就是多个终点和单个起点了,于是直接最短路。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
using namespace std; 
const int N=20005; 
const int INF=9999999; 
typedef pair<int,int>seg; 
priority_queue<seg,vector,greater >q;    
int begin,end,d[N],head[N],u[N],v[N],w[N],next[N],n,m,a,b,c,k;
bool vis[N]; 
void build(){ 
    memset(head,-1,sizeof(head));
    for(int e=1;e<=m;e++){ 
        scanf("%d%d%d",&v[e],&u[e],&w[e]); 
        next[e]=head[u[e]]; head[u[e]]=e; 
    
}    
void Dijkstra(int src){ 
    memset(vis,0,sizeof(vis)); 
    for(int i=0;i<=n;i++) d[i]=INF; 
    d[src]=0; 
    q.push(make_pair(d[src],src)); 
    while(!q.empty()){ 
        seg now=q.top(); q.pop(); 
        int x=now.second; 
        if(vis[x]) continue; vis[x]=true
        for(int e=head[x];e!=-1;e=next[e])
        if(d[v[e]]>d[x]+w[e]){ 
            d[v[e]]=d[x]+w[e]; 
            q.push(make_pair(d[v[e]],v[e])); 
        }  
    
}     
int main(){ 
    while(~scanf("%d%d%d",&n,&m,&begin)){
        build(); int min=INF;
        scanf("%d",&k);
        Dijkstra(begin);
        for(int i=0;i<k;i++){
            scanf("%d",&end);
            min=d[end]<min?d[end]:min;
        }
        if(min==INF)puts("-1");
        else printf("%d\n",min);
    
    return 0; 
}

HDU 2680 Choose the best route,布布扣,bubuko.com

HDU 2680 Choose the best route

原文:http://www.cnblogs.com/forever97/p/3619101.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!