zoukankan      html  css  js  c++  java
  • POJ 3615 Cow Hurdles

    http://poj.org/problem?id=3615

    floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边

    那么输入的时候可以直接把dist[i][j] 当作i j 之间的边进行输入

    转移方程 dist[i][j] = max(dist[i][j], min(dist[i][k], dist[k][j]))

     1 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 #define INF 0x3fffffff
     5 using namespace std;
     6 const int maxn = 305;
     7 int Map[maxn][maxn];
     8 
     9 int dist[maxn][maxn];//dist[i][j]表示 i 到 j 的最大边是多少?
    10 //转移方程 dist[i][j] = min( dist[i][j] ,max(dist[i][k], dist[k][j])) ---> "**"
    11 //
    12 //与floyd的区别 dist是 i-j的最短'距离'
    13 int main()
    14 {
    15     int m, n, k;
    16     scanf("%d%d%d", &m, &n, &k);
    17     for(int i = 1; i <= m; i++)
    18         for (int j = 1; j <= m; j++)
    19             dist[i][j] = INF;
    20     for (int i = 0; i < n; i++)
    21     {
    22         int from, to, cost;
    23         scanf("%d%d%d", &from, &to, &cost);
    24         dist[from][to] = cost;
    25     }
    26     for(int i = 1; i <= m; i++)
    27     {
    28         for (int j = 1; j <= m; j++)
    29         {
    30             for (int k = 1; k <= m; k++)
    31             {
    32                // if (dist[j][k] == -1) dist[j][k] = max(dist[j][i], dist[i][k]);
    33                 //else
    34                     dist[j][k] = min(dist[j][k], max(dist[j][i], dist[i][k]));
    35             }
    36         }
    37     }
    38     for (int i = 0; i < k; i++)
    39     {
    40         int from, to;
    41         scanf("%d%d", &from, &to);
    42         if (dist[from][to] == INF) cout << -1 << endl;
    43         else  cout << dist[from][to] << endl;
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    BNUOJ 12756 Social Holidaying(二分匹配)
    HDU 1114 Piggy-Bank(完全背包)
    HDU 2844 Coins (多重背包)
    HDU 2602 Bone Collector(01背包)
    HDU 1171 Big Event in HDU(01背包)
    HDU 2571 命运 (入门dp)
    HDU 1069 Monkey and Banana(最长递减子序列)
    HDU 1160 FatMouse's Speed (最长上升子序列)
    HDU 2594 KMP
    POJ 3783 Balls --扔鸡蛋问题 经典DP
  • 原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6745509.html
Copyright © 2011-2022 走看看