• hihoCoder#1139 二分·二分答案


    原题地址

    挺简单一道题,结果因为一时傻逼写错一个小地方,导致WA成狗了_(:з」∠)_

    代码:

     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 
     5 using namespace std;
     6 
     7 #define MAX_V 10008
     8 #define MAX_E 200008
     9 
    10 int N, M, K, T;
    11 int f[MAX_V];
    12 bool v[MAX_V];
    13 int n[MAX_E];
    14 int w[MAX_E];
    15 int t[MAX_E];
    16 int max_w = 1;;
    17 int min_w = 1000000;
    18 
    19 bool check(int l) {
    20   queue<pair<int, int> > q;
    21   memset(v, 0, sizeof(v));
    22   q.push(pair<int, int>(1, K));
    23   while (!q.empty()) {
    24     pair<int, int> p = q.front();
    25     q.pop();
    26     if (p.first == T && p.second >= 0)
    27       return true;
    28     if (p.second == 0 || v[p.first])
    29       continue;
    30     v[p.first] = true;
    31     for (int i = f[p.first]; i != 0; i = n[i]) {
    32       if (w[i] > l || v[t[i]])
    33         continue;
    34       q.push(pair<int, int>(t[i], p.second - 1));
    35     }
    36   }
    37   return false;
    38 }
    39 
    40 int find(int l, int r) {
    41   int res = r;
    42   while (l <= r) {
    43     int m = (l + r) / 2;
    44     if (check(m)) {
    45       res = m;
    46       r = m - 1;
    47     }
    48     else
    49       l = m + 1;
    50   }
    51   return res;
    52 }
    53 
    54 int main() {
    55   memset(n, 0, sizeof(n));
    56   scanf("%d%d%d%d", &N, &M, &K, &T);
    57   for (int i = 0, j = 0; i < M; i++) {
    58     int a, b, c;
    59     scanf("%d%d%d", &a, &b, &c);
    60     min_w = min(min_w, c);
    61     max_w = max(max_w, c);
    62     j++;
    63     w[j] = c;
    64     t[j] = b;
    65     n[j] = f[a];
    66     f[a] = j;
    67     j++;
    68     w[j] = c;
    69     t[j] = a;
    70     n[j] = f[b];
    71     f[b] = j;
    72   }
    73   if (T == 1)
    74     printf("0
    ");
    75   else
    76     printf("%d
    ", find(min_w, max_w));
    77   return 0;
    78 }
  • 相关阅读:
    仙人球的残影
    Calculate the formula
    自定义代码段
    getter-setter方法练习
    封装
    Xcode文档安装
    Xcode模板修改
    匿名对象
    OC多文件开发介绍
    #pragma mark指令
  • 原文地址:https://www.cnblogs.com/boring09/p/4415792.html
Copyright © 2020-2023  润新知