• Floyd求最小环


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

     1 Problem : 1599 ( find the mincost route )     Judge Status : Accepted
     2 RunId : 10374468    Language : C++    Author : l1285556798
     3 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
     4 
     5 #include<stdio.h>
     6 #define INF 0xffffff
     7 int map[301][301],dist[301][301];
     8 int n,m,ans;
     9 void floyd()
    10 {
    11     for(int i=1;i<=n;i++)
    12         for(int j=1;j<=n;j++)
    13         dist[i][j]=map[i][j];
    14     for(int k=1;k<=n;k++)
    15     {
    16         for(int i=1;i<=n;i++)
    17         {
    18             for(int j=1;j<=n;j++)
    19             {
    20                 if(i!=j&&j!=k&&k!=i&&dist[i][j]!=INF&&map[j][k]!=INF&&map[k][i]!=INF&&dist[i][j]+map[j][k]+map[k][i]<ans)
    21                     ans=dist[i][j]+map[j][k]+map[k][i];
    22             }
    23         }
    24         for(int i=1;i<=n;i++)
    25         {
    26             for(int j=1;j<=n;j++)
    27             {
    28                 if(dist[i][k]!=INF&&dist[k][j]!=INF&&dist[i][k]+dist[k][j]<dist[i][j])
    29                     dist[i][j]=dist[i][k]+dist[k][j];   //注意取不同点
    30             }
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     while(scanf("%d%d",&n,&m)!=EOF)
    37     {
    38         for(int i=1;i<=n;i++)
    39             for(int j=1;j<=n;j++)
    40         {
    41             if(i!=j)
    42                 map[i][j]=INF;
    43             else
    44                 map[i][j]=0;
    45         }
    46         int a,b,c;
    47         for(int i=0;i<m;i++)
    48         {
    49             scanf("%d%d%d",&a,&b,&c);
    50             if(a!=b&&c<map[a][b])
    51                 map[a][b]=map[b][a]=c;
    52         }
    53         ans=INF;
    54         floyd();
    55         if(ans==INF)
    56             printf("It's impossible.
    ");
    57         else
    58             printf("%d
    ",ans);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    爬虫之暴力字典生成器
    爬虫之自动生成url
    数字、大小写字母的字符编码
    对avalonjs的研究
    求墙之间有多少水洼
    2.在centos7虚拟机搭建nginx网站
    P1250 种树
    暂时用笔记
    羊村的OI题解
    P1083 借教室
  • 原文地址:https://www.cnblogs.com/lyf123456/p/3618831.html
Copyright © 2020-2023  润新知