zoukankan      html  css  js  c++  java
  • BZOJ3417 Poi2013 Tales of seafaring

    一眼,什么神题。。。

    然后发现对每个点无脑bfs一下是O(mn)的。。。额好吧Σ( ° △ °|||)

    觉得clrs的写法很好呢,学习了!

     1 /**************************************************************
     2     Problem: 3417
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:7648 ms
     7     Memory:12916 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11  
    12 using namespace std;
    13 const int N = 10010;
    14 const int K = 1000010;
    15  
    16 struct edge {
    17   int next, to;
    18   edge() {}
    19   edge(int _n, int _t) : next(_n), to(_t) {}
    20 } e[N];
    21  
    22 struct Edge {
    23   int next, to, v;
    24   Edge() {}
    25   Edge(int _n, int _t, int _v) : next(_n), to(_t), v(_v) {}
    26 } E[K];
    27  
    28 int first[N], First[N], tot, Tot;
    29  
    30 struct queue {
    31   int x, y;
    32   queue() {}
    33   queue(int _x, int _y) : x(_x), y(_y) {}
    34 } q[N];
    35  
    36 int n, m, Q, l, r;
    37 int pos[N][2], dis[N][2];
    38  
    39 int read() {
    40   int x = 0;
    41   char ch = getchar();
    42   while (ch < '0' || '9' < ch)
    43     ch = getchar();
    44   while ('0' <= ch && ch <= '9')
    45     (x *= 10) += ch - '0', ch = getchar();
    46   return x;
    47 }
    48  
    49 inline void add_edges(int x, int y) {
    50   e[++tot] = edge(first[x], y), first[x] = tot;
    51   e[++tot] = edge(first[y], x), first[y] = tot;
    52 }
    53  
    54 inline void Add_Edge(int x, int y, int z) {
    55   E[++Tot] = Edge(First[x], y, z), First[x] = Tot;
    56 }
    57  
    58 inline void update(int x, int y, int z, int i) {
    59   if (pos[x][y] < i)
    60     pos[x][y] = i, dis[x][y] = z, q[++r] = queue(x, y);
    61 }
    62  
    63 int main() {
    64   int i, j, x, y, z, X, Y;
    65   n = read(), m = read(), Q = read();
    66   for (i = 1; i <= m; ++i)
    67     add_edges(read(), read());
    68   for (i = 1; i <= Q; ++i) {
    69     x = read(), y = read(), z = read();
    70     Add_Edge(x, y, z);
    71   }
    72   for (i = 1; i <= n; ++i) if (First[i]) {
    73       l = 1, r = 0, update(i, 0, 0, i);
    74       while (l <= r) {
    75     X = q[l].x, Y = q[l].y, ++l;
    76     for (x = first[X]; x; x = e[x].next)
    77       update(e[x].to, Y ^ 1, dis[X][Y] + 1, i);
    78       }
    79       for (x = First[i]; x; x = E[x].next)
    80     if ((E[x].to != i || first[i]) && pos[E[x].to][E[x].v & 1] == i && dis[E[x].to][E[x].v & 1] <= E[x].v) E[x].to = 0;
    81     }
    82   for (i = 1; i <= Tot; ++i)
    83     puts(E[i].to ? "NIE" : "TAK");
    84   return 0;
    85 }
    View Code
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    luogu P2639 [USACO09OCT]Bessie的体重问题Bessie's We…
    1001. 害死人不偿命的(3n+1)猜想 (15)
    1003. 我要通过!(20)
    1002. 写出这个数 (20)
    《C语言程序设计(第四版)》阅读心得(一)
    1006. 换个格式输出整数 (15)
    背包问题之多重背包
    背包问题之完全背包
    背包问题之0-1背包
    动态规划例题
  • 原文地址:https://www.cnblogs.com/rausen/p/4255163.html
Copyright © 2011-2022 走看看