洛谷P1396 营救 图论

洛谷P1396 营救

图论 dijkstra + 堆优化

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <iostream> 
 9 #include <queue>
10 #include <vector>
11 using namespace std ; 
12 
13 const int maxn = 10011 ,maxm = 20011,inf = 1e9 ; 
14 int n,m,s,t,cnt,x,y,v,ans ; 
15 struct node{
16     int to,pre,val ; 
17 };
18 node e[2*maxm] ;  
19 int dist[maxn],vis[maxn],head[maxn] ; 
20 
21 struct data{
22     int dist,pos ;  
23 }; 
24 
25 struct cmp{
26     bool operator() (data a,data b) 
27     {
28         return a.dist > b.dist ; 
29     }
30 };
31 priority_queue <data,vector<data>,cmp> Q ; 
32 
33 inline void addedge(int x,int y,int v) 
34 {
35     e[++cnt] = (node){ y,head[x],v } ;
36     head[x] = cnt ; 
37 }
38 
39 inline int dij(int s,int t) 
40 {
41     int u,v ; 
42     for(int i=1;i<=n;i++) dist[ i ] = inf ; 
43     for(int i=1;i<=n;i++) vis[ i ] = false ; 
44     data p ; 
45     dist[ s ] = 0 ; 
46     p = (data){ 0,s } ; 
47     Q.push(p) ; 
48     while(!Q.empty()) 
49     {
50         p = Q.top() ; 
51         Q.pop() ;  
52         //while(!Q.empty()&&vis[Q.top().pos]) Q.pop() ; 
53         if(vis[p.pos]) continue ; 
54         u = p.pos ; 
55         vis[ u ] = 1 ; 
56         for(int i=head[ u ];i;i = e[ i ].pre) 
57         {
58             v = e[ i ].to ; 
59             dist[ v ] = min( max(e[ i ].val,dist[u]) , dist[ v ] ) ; 
60             p = (data){ dist[v],v } ; 
61             Q.push(p) ;  
62         }
63     }
64     return dist[ t ] ; 
65 }
66 
67 int main() 
68 {
69     scanf("%d%d%d%d",&n,&m,&s,&t) ; 
70     for(int i=1;i<=m;i++) 
71     {
72         scanf("%d%d%d",&x,&y,&v) ; 
73         addedge(x,y,v) ; 
74         addedge(y,x,v) ; 
75     }
76     
77     ans = dij(s,t) ;  
78     printf("%d\n",ans) ; 
79     return 0 ; 
80 }

 

posted @ 2017-06-12 10:44  third2333  阅读(178)  评论(0编辑  收藏  举报