zoukankan      html  css  js  c++  java
  • HDU-1874 畅通工程续

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874

    思路:Dijkstra模板题

    代码:

     1 #include<bits/stdc++.h>
     2 
     3 #define inf 0x3f3f3f3f
     4 using namespace std;
     5  
     6 typedef long long ll;
     7 typedef long double ld;
     8  
     9 const int M = int(1e5)*3 + 5;
    10 const int mod = 10007;
    11 
    12 struct edge{
    13     int to;
    14     int w;
    15     int next;
    16 };
    17 edge edges[M];
    18 int head[M];
    19 int cnt;
    20 void add(int u,int v,int w){
    21     edges[cnt].to=v;
    22     edges[cnt].w=w;
    23     edges[cnt].next=head[u];
    24     head[u]=cnt++;
    25     edges[cnt].to=u;
    26     edges[cnt].w=w;
    27     edges[cnt].next=head[v];
    28     head[v]=cnt++;
    29 }
    30 
    31 struct node{
    32     int v;
    33     int cost;
    34     node(int _v=0,int _c=0):v(_v),cost(_c){}
    35     bool operator <(const node& b)const{
    36         return cost>b.cost;
    37     }
    38 };
    39 int n,m;
    40 int u,v,w;
    41 int dis[M];
    42 bool vis[M];
    43 priority_queue<node> pq;
    44 void dijkstra(int n,int start){
    45     memset(vis,0,sizeof(vis));
    46     // memset(dis,0x3f,sizeof(dis));
    47     for(int i=0;i<n;i++) dis[i]=inf;
    48     dis[start]=0;
    49     while(!pq.empty()) pq.pop();
    50     pq.push(node(start,0));
    51     node tem;
    52     while(!pq.empty()){
    53         tem=pq.top();
    54         pq.pop();
    55         int u=tem.v;
    56         if(vis[u]) continue;
    57         vis[u]=1;
    58         for(int i=head[u];~i;i=edges[i].next){
    59             int v=edges[i].to;
    60             if(dis[v]>dis[u]+edges[i].w){
    61                 dis[v]=dis[u]+edges[i].w;
    62                 pq.push(node(v,dis[v]));
    63             }
    64         }
    65     }
    66 }
    67 int main(){
    68     while(~scanf("%d%d",&n,&m)){
    69         cnt=0;
    70         memset(head,-1,sizeof(head));
    71         for(int i=0;i<m;i++){
    72             scanf("%d%d%d",&u,&v,&w);
    73             add(u,v,w);
    74         }
    75         int s,t;
    76         cin >> s >> t;
    77         dijkstra(n,s);
    78         printf("%d
    ",dis[t]==inf?-1:dis[t]);
    79     }
    80 
    81     return 0;
    82 }
  • 相关阅读:
    启发式搜索学习~~
    bzoj1032
    bzoj1037
    bzoj1029
    codevs1081 线段树练习 2
    bzoj1006
    bzoj1003
    Codeforces 607B Zuma
    20155326《网络对抗》网络欺诈技术防范
    20155326刘美岑 Exp6 信息收集与漏洞扫描
  • 原文地址:https://www.cnblogs.com/harutomimori/p/12770562.html
Copyright © 2011-2022 走看看