• HDOJ 1217 Floyed Template


    解题思路:
    1.map简单应用
    2.Floyd算法的变形,之后判断dis[i][i],如果大于1,则存在利润!

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <cstring>
     5 #include<map>
     6 using namespace std;
     7 
     8 map<string,int>name;
     9 const int INF = 1000000;
    10 const int MAXSIZE = 1005;
    11 const int MAXN = 30;
    12 double g[MAXN][MAXN];
    13 
    14 //dis[][]记录任意两点间的最短路径,初始的dis[][]记录直接路径
    15 void floyed(double dis[][MAXN],int n){//节点从1~n编号
    16     int i,j,k;
    17     for(k = 1; k <= n; k++)
    18        for(i = 1; i <= n; i++)
    19          for(j = 1; j <= n; j++)
    20              if(dis[i][j] < dis[i][k] * dis[k][j])
    21                  dis[i][j] = dis[i][k] * dis[k][j];
    22 }
    23 int main(){
    24     int n,i,m,j;
    25     string str1,str2;
    26     double r;
    27     int iCase = 0;
    28     while(scanf("%d",&n),n){
    29         iCase++;
    30         for(i = 1; i <= n; i++){
    31             cin>>str1;
    32             name[str1] = i;
    33         }
    34         for(i = 1; i <= n; i++)
    35            for(j = 1; j <= n; j++){
    36                if(i == j) g[i][j] = 1;
    37                else g[i][j] = 0;
    38            }
    39         scanf("%d",&m);
    40         while(m--){
    41             cin>>str1>>r>>str2;
    42             g[name[str1]][name[str2]] = r;
    43         }
    44         
    45         floyed(g,n);
    46         
    47         bool flag = false;
    48         for(i = 1; i <= n; i++){
    49           if(g[i][i] > 1){
    50             flag = true;
    51             break;
    52           }
    53         }
    54         if(flag)  printf("Case %d: Yes
    ",iCase);
    55         else printf("Case %d: No
    ",iCase);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    三数之和
    盛最多水的容器
    正则表达式匹配
    最长回文子串
    寻找两个有序数组的中位数
    2、二维数组中的查找
    1、找出数组中重复的数字
    mongodb的下载地址
    提取快捷方式的图标资源问题
    一条数据引发的问题
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3694735.html
Copyright © 2020-2023  润新知