• Hdu 1874


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

    在本题目中运用两种最短路算法来解决实际问题

    在弗洛伊德算法中判断是否是负循环只需要判断i==j的情况下的数是否为-1即可。

    1、迪杰斯特拉算法

     1 #include<iostream>
     2 #include<string.h>
     3 using namespace std;
     4 const int MAX_N = 1010;
     5 const int INF = 0x3f3f3f3f;
     6 bool v[MAX_N];
     7 int l[220];
     8 int c[220][220];
     9 int n,m;
    10 void Ds(int x)
    11 {
    12     for (int i = 0; i < n; i++)
    13     {
    14         l[i] = INF;
    15         v[i] = false;
    16     }
    17     l[x] = 0;
    18     for (int j = 0; j < n; j++)
    19     {
    20         int k = -1;
    21         int minv = INF;
    22         for (int i = 0; i < n; i++)
    23         {
    24             if (!v[i] && l[i] < minv)
    25             {
    26                 minv = l[i];
    27                 k = i;
    28             }
    29         }
    30         if (k == -1)
    31         {
    32             break;
    33         }
    34         v[k] = true;
    35         for (int i = 0; i < n; i++)
    36         {
    37             if (!v[i] && l[k] + c[k][i] < l[i])
    38             {
    39                 l[i] = l[k] + c[k][i];
    40             }
    41         }
    42     }
    43 }
    44 int main()
    45 {
    46     while(cin>>n>>m)
    47     {
    48         memset(c,INF,sizeof(c));
    49         for(int i=0; i<m ;i++)
    50         {
    51             int A,B,X;
    52             cin>>A>>B>>X;
    53             c[A][B]=min(c[A][B],X);
    54             c[B][A]=min(c[A][B],X);
    55         }
    56         int S,T;
    57         cin>>S>>T;
    58         Ds(S);
    59         if(l[T]==INF)
    60         {
    61             cout<<"-1"<<endl;
    62         }
    63         if(l[T]!=INF)
    64         cout<<l[T]<<endl;
    65     }
    66 }

    2、弗洛伊德算法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 #define inf 0x3f3f3f3f
     7 
     8 int map1[300][300];
     9 int main()
    10 {
    11     int n,m;
    12     while(cin>>n>>m)
    13     {
    14         for(int i=0;i<n;i++)
    15     {
    16         for(int j=0;j<n;j++)
    17         {
    18             if(i==j)
    19             {
    20                 map1[i][j]=0;
    21             }
    22             else
    23                 map1[i][j]=inf;
    24         }
    25     }
    26     int a,b,c;
    27     for(int i=0;i<m;i++)
    28     {
    29          cin>>a>>b>>c;
    30          map1[a][b]=min(map1[a][b],c);
    31          map1[b][a]=min(map1[b][a],c);
    32     }
    33     for(int k=0;k<n;k++)
    34     {
    35         for(int i=0;i<n;i++)
    36         {
    37             for(int j=0;j<n;j++)
    38             {
    39                 if(map1[i][j]>map1[i][k]+map1[k][j])
    40                 {
    41                    map1[i][j]=map1[i][k]+map1[k][j];
    42                 }
    43             }
    44         }
    45     }
    46     int s,t;
    47     cin>>s>>t;
    48     if(map1[s][t]==inf)
    49     {
    50         cout<<-1<<endl;
    51     }
    52     else
    53         cout<<map1[s][t]<<endl;
    54     }
    55     
    56     return 0;
    57 }
  • 相关阅读:
    蓝凌OA 后台URL跳转(鸡肋0day)
    蓝凌后台注入分析
    蓝凌ssrf+xmldecoder
    shiro550反序列化复现
    BCEL ClassLoader加载字节码
    TemplatesImple链加载字节码
    ysoserial Commons Collections3反序列化研究
    Xstream远程代码执行(CVE-2020-26217)复现分析
    Java安全之命令执行(二)
    Java安全之命令执行(一)
  • 原文地址:https://www.cnblogs.com/Yinchen-One/p/8541621.html
Copyright © 2020-2023  润新知