zoukankan      html  css  js  c++  java
  • HDU-1599 find the mincost route

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

    思路:floyd求最小环模板,不知道为什么用dijkstra超时了,inf设为0x7ffffff防止爆int

    代码:

     1 #include<bits/stdc++.h>
     2 #define inf 0x7ffffff
     3 #define ms(a) memset(a,0,sizeof(a))
     4 using namespace std;
     5 typedef long long ll;
     6 
     7 const int M = int(1e2)*5 + 5;
     8 const int mod = int(1e9) + 7;
     9 
    10 
    11 int n,m;
    12 
    13 int g[M][M];
    14 int dis[M][M];
    15 void init(){
    16     for(int i=0;i<=n;i++){
    17         for(int j=0;j<=n;j++){
    18             g[i][j]=dis[i][j]=inf;
    19         }
    20     }
    21 }
    22 
    23 void floyd(){
    24     int ans=inf;
    25     for(int k=1;k<=n;k++){
    26         for(int i=1;i<k;i++){
    27             for(int j=i+1;j<k;j++){
    28                 ans=min(ans,g[i][k]+g[k][j]+dis[i][j]);
    29             }
    30         }
    31         for(int i=1;i<=n;i++){
    32             for(int j=1;j<=n;j++){
    33                 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
    34             }
    35         }
    36     }
    37     if(ans==inf) cout<<"It's impossible."<<endl;
    38     else cout<<ans<<endl;
    39 }
    40 int main(){
    41     while(cin>>n>>m){
    42         init();
    43         for(int i=0;i<m;i++){
    44             int u,v,w;
    45             cin>>u>>v>>w;
    46             g[u][v]=min(g[u][v],w);
    47             g[v][u]=dis[u][v]=dis[v][u]=g[u][v];
    48         }
    49         floyd();
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    CodeForces
    4.15随笔
    oracle新建用户并赋予权限等
    catch时,获取异常信息
    ORACLE 判断是否为数字类型
    UNION ALL用法
    2019.11.7随笔
    oracle 查询锁表和解锁
    2019.11.1随笔
    oracle拼接子查询返回的多个结果
  • 原文地址:https://www.cnblogs.com/harutomimori/p/12770600.html
Copyright © 2011-2022 走看看