zoukankan      html  css  js  c++  java
  • 洛谷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
    ",ans) ; 
    79     return 0 ; 
    80 }
  • 相关阅读:
    2.8Java专项测试复盘
    我的第一篇博客
    VS2010调试汇编
    socket学习
    DLL 共享数据学习
    PE学习
    char*,const char*和string的相互转换 + 三种版本字符串
    unresolved external symbol “symbol”(不确定的外部“符号”)。
    深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p
    volatile学习
  • 原文地址:https://www.cnblogs.com/third2333/p/6991747.html
Copyright © 2011-2022 走看看