zoukankan      html  css  js  c++  java
  • hdu1874 畅通工程续

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

     1 //标准最短路模板
     2 //需要注意的是两点间可能有多组
     3 //需要取最短的
     4 #include<iostream>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<string.h>
     8 #include<stdlib.h>
     9 #include<limits.h>
    10 using namespace std;
    11 const int N=210;
    12 int a[N][N];
    13 int use[N];
    14 int d[N];
    15 int n,m;
    16 void dijkstra(int u)//u起点
    17 {
    18     memset(use,0,sizeof(use));
    19     for(int i=0;i<n;i++)
    20     d[i]=a[u][i];//初始化
    21     use[u]=1;//标记使用
    22     d[u]=0;//u u距离为0
    23     for(int i=1;i<n;i++)
    24     {
    25         int min=INT_MAX;
    26         int v=-1;
    27         for(int w=0;w<n;w++)//找最短的路
    28         {
    29             if(!use[w] && d[w]<min)
    30             {
    31                 min=d[w];
    32                 v=w;
    33             }
    34         }
    35         if(v!=-1)//没有路径则不更新
    36         {
    37             use[v]=1;
    38             for(int w=0;w<n;w++)
    39             {
    40                 if(!use[w] && (a[v][w]<INT_MAX))//做路径判断
    41                 {
    42                     if(d[w] > min+a[v][w])
    43                     {
    44                         d[w]=min+a[v][w];
    45                     }
    46                 }
    47             }
    48         }
    49     }
    50 }
    51 int main()
    52 {
    53     //freopen("in.txt","r",stdin);
    54     while(~scanf("%d%d",&n,&m)&&n&&m)
    55     {
    56         memset(a,0,sizeof(a));
    57         int x,y,c;
    58         for(int i=0;i<n;i++)
    59         {
    60             for(int j=0;j<n;j++)
    61             a[i][j]=INT_MAX;
    62         }
    63         for(int i=0;i<m;i++)
    64         {
    65             scanf("%d%d%d",&x,&y,&c);
    66             if(c<a[x][y])//注意判断多组情况
    67             a[x][y]=a[y][x]=c;
    68         }
    69         int start,end;
    70         scanf("%d%d",&start,&end);
    71         dijkstra(start);
    72         if(d[end]==INT_MAX)
    73         {
    74             printf("-1
    ");
    75         }
    76         else
    77         printf("%d
    ",d[end]);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    win10下python环境变量设置
    c++ primer第15章这几个例子中的构造函数形式不太理解
    ++与*
    C++符号优先级
    56-Remove Linked List Elements
    55. Binary Tree Preorder Traversal
    54. Flatten Binary Tree to Linked List
    野指针--内存泄漏--缓存区溢出--栈溢出
    数组指针和指针数组的区别
    53-Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4539392.html
Copyright © 2011-2022 走看看