zoukankan      html  css  js  c++  java
  • Codeforces346D. Robot Control

    D. Robot Control
    time limit per test
    6 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The boss of the Company of Robot is a cruel man. His motto is "Move forward Or Die!". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called "Three Laws of Robotics":

    • Law 1. The Robot will destroy itself when it visits a vertex of the graph which it has already visited.
    • Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero).
    • Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph.

    Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex s to vertex t in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex t, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from s to t, she can always find a way to reach the goal (whatever the vertex t has the outdegree of zero or not).

    Sample 2

    However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem.

    Input

    The first line contains two integers n (1 ≤ n ≤ 106) — the number of vertices of the graph, and m (1 ≤ m ≤ 106) — the number of edges. Then m lines follow, each with two integers ui and vi (1 ≤ ui, vi ≤ n; vi ≠ ui), these integers denote that there is a directed edge from vertex ui to vertex vi. The last line contains two integers s and t (1 ≤ s, t ≤ n).

    It is guaranteed that there are no multiple edges and self-loops.

    Output

    If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1.

    Examples
    Input
    4 6
    1 2
    2 1
    1 3
    3 1
    2 4
    3 4
    1 4
    Output
    1
    Input
    4 5
    1 2
    2 1
    1 3
    2 4
    3 4
    1 4
    Output
    1
    Note

    Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can choose the "bad" outgoing edge (return to vertex 1) according Law 3. So, the answer is one.

    【题解】

    dp[u]表示从u这个点到终点需要的最小代价

    dp[u] = min(max(dp[v]), min(dp[u]) + 1), dp[t] = 1, u - > v

    可以用SPFA转移

    对于点u,用u去松弛u的入边的min(dp[u]) + 1,用u的出边的点去松弛u的max(dp[v])

    时间复杂度O(玄学)

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <algorithm>
      6 #include <sstream>
      7 #include <vector>
      8 #include <string>
      9 #include <cmath> 
     10 #include <queue>
     11 #define min(a, b) ((a) < (b) ? (a) : (b))
     12 #define max(a, b) ((a) > (b) ? (a) : (b))
     13 
     14 inline void swap(int &a, int &b)
     15 {
     16     int tmp = a;a = b;b = tmp;
     17 }
     18 
     19 inline void read(int &x)
     20 {
     21     x = 0;char ch = getchar(), c = ch;
     22     while(ch < '0' || ch > '9')c = ch, ch = getchar();
     23     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
     24     if(c == '-')x = -x;
     25 }
     26 
     27 const int INF = 0x3f3f3f3f;
     28 const int MAXN = 1000000 + 10;
     29 const int MAXM = 1000000 + 10;
     30 
     31 struct Edge
     32 {
     33     int u,v,nxt;
     34     Edge(int _u, int _v, int _nxt){u = _u;v = _v;nxt = _nxt;}
     35     Edge(){}
     36 }edge1[MAXM], edge2[MAXN];
     37 int head1[MAXN], head2[MAXN], cnt1, cnt2;
     38 inline void insert(int a, int b)
     39 {
     40     edge1[++cnt1] = Edge(a,b,head1[a]);
     41     head1[a] = cnt1;
     42     edge2[++cnt2] = Edge(b,a,head2[b]);
     43     head2[b] = cnt2;
     44 }
     45 
     46 int n,m,s,t,dp[MAXN],b[MAXN];
     47 std::queue<int> q;
     48 
     49 /*
     50 dp[u] = min(min(dp[v]) + 1, max(dp[v])) 
     51 */
     52 
     53 void SPFA()
     54 {
     55     b[t] = 1;memset(dp, 0x3f, sizeof(dp));dp[t] = 0;q.push(t);
     56     while(q.size())
     57     {
     58         int u = q.front();q.pop();b[u] = 0;
     59         for(register int pos = head2[u];pos;pos = edge2[pos].nxt)
     60         {
     61             int v = edge2[pos].v;
     62             if(dp[u] + 1 < dp[v])
     63             {
     64                 dp[v] = dp[u] + 1;
     65                 if(!b[v])
     66                 {
     67                     b[v] = 1;
     68                     q.push(v);
     69                 }
     70             }
     71         }
     72         int tmp = 0;
     73         for(register int pos = head1[u];pos;pos = edge1[pos].nxt) tmp = max(tmp, dp[edge1[pos].v]);
     74         if(tmp < dp[u])
     75         {
     76             dp[u] = tmp;
     77             if(!b[u])
     78             {
     79                 b[u] = 1;
     80                 q.push(u);
     81             }
     82         }
     83     }
     84 }
     85 
     86 int main()
     87 {
     88     read(n), read(m);
     89     for(register int i = 1;i <= m;++ i)
     90     {
     91         int tmp1,tmp2;
     92         read(tmp1), read(tmp2);
     93         insert(tmp1, tmp2);
     94     }
     95     read(s), read(t);
     96     SPFA();
     97     if(dp[s] == INF)dp[s] = -1;
     98     printf("%d
    ", dp[s]);
     99     return 0;
    100 } 
    Codeforces346D
  • 相关阅读:
    如何使样式CSS不被覆盖 !important
    PHP5中数组函数总结篇
    优化php效率,提高php性能的一些方法
    windows2003 系统下不能识别移动硬盘解决方法
    Sql Server 2000索引问题?
    在网页中调用本地的应用程序
    Sql Server资料收集(摘自http://www.itpub.net)
    利用CSS控制打印
    C#.NET 中的类型转换
    卓越领导力素质训练心得
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7715898.html
Copyright © 2011-2022 走看看