zoukankan      html  css  js  c++  java
  • EOJ 1028 路由器

    http://acm.cs.ecnu.edu.cn/problem.php?problemid=1028

    这题以字符串作为节点,有一个字符串映射到节点序号1……N的问题,可以用map<string,int>来解决

    这题数据不大,所以可以直接用floyd算法

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <map>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 const int INF = 9999999;
    10 int m[102][102];
    11 map<string, int>my;
    12 int n;
    13 
    14 void floyd()
    15 {
    16     for (int k = 1; k <= n; k++)
    17         for (int i = 1; i <= n; i++)
    18             for (int j = 1; j <= n; j++)
    19                 m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
    20 }
    21 
    22 int main()
    23 {
    24     int T;
    25     while (cin >> n >> T)
    26     {
    27         my.clear();
    28         int cnt = 1;
    29         for (int i = 1; i <= n; i++)
    30             for (int j = 1; j <= n; j++)
    31                 m[i][j] = INF;
    32         string a, b;
    33         int t;
    34         while (T--)
    35         {
    36             cin >> a >> b >> t;
    37             if (my[a] == 0)
    38                 my[a] = cnt++;
    39             if (my[b] == 0)
    40                 my[b] = cnt++;
    41             m[my[a]][my[b]] = t;
    42             m[my[b]][my[a]] = t;
    43         }
    44         floyd();
    45         cin >> t;
    46         while (t--)
    47         {
    48             cin >> a >> b;
    49             int ans;
    50             if (!my[a] || !my[b])
    51                 ans = -1;
    52             else
    53             {
    54                 if (m[my[a]][my[b]] == INF)
    55                     ans = -1;
    56                 else
    57                     ans = m[my[a]][my[b]];
    58             }
    59             cout << ans << endl;
    60         }
    61     }    
    62 }
    View Code
  • 相关阅读:
    ajax post 时 form数据serialize()
    dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法
    TImage 的一些操作
    AOP
    SSL、数字签名、CA 工作原理
    RESTFUL
    tomcat
    Hibernate
    设计模式
    Spring配置
  • 原文地址:https://www.cnblogs.com/KimKyeYu/p/3133252.html
Copyright © 2011-2022 走看看