zoukankan      html  css  js  c++  java
  • NOIp2014 寻找道路

    这道题本身真的不怎么难,但是

    "路径上所有的点的出边所指向的点都直接或间接与终点连通"

    这句话真的是太ex了。。

    我隐隐约约记得当年做这道题时的懵逼,看来当时的代码确实是没什么问题,只是因为理解上的误差

    教训:好好看题,看明白了再做,要不然一切都是白干。

    题干

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 #include<algorithm>
      6 
      7 using namespace std;
      8 
      9 typedef long long ll;
     10 
     11 const int Maxn = 1e4+10,Maxm = 2e5+10;
     12 
     13 ll read(){
     14     ll ans = 0;
     15     char last = ' ',ch = getchar();
     16     while(ch < '0'||ch > '9')last = ch,ch = getchar();
     17     while('0' <= ch&&ch <= '9')ans = ans*10+ch-'0',ch = getchar();
     18     if(last == '-')return -ans;return ans;
     19 }
     20 
     21 struct Edge{
     22     int to,ne;
     23 }edges[2][Maxm],e;
     24 
     25 struct Node{
     26     int to,d;
     27     bool operator <(const Node& x)const{
     28         return d > x.d;
     29     }
     30 };
     31 
     32 int first[2][Maxn];
     33 int d[Maxn],vis[Maxn],v2[Maxn];
     34 int cnte = 0,n,m,u,v,s,t;
     35 
     36 void add_edge(int fr,int to){
     37     edges[1][++cnte] = (Edge){to,first[1][fr]};
     38     edges[0][cnte] = (Edge){fr,first[0][to]};
     39     first[1][fr] = first[0][to] = cnte;
     40 }
     41 
     42 void dfs(int s){
     43     vis[s] = 1;
     44     for(int i = first[0][s];i;i = edges[0][i].ne){
     45         e = edges[0][i];
     46         if(vis[e.to])continue;
     47         dfs(e.to);
     48     }
     49 }
     50 
     51 void del(int s){
     52     vis[s] = 0;
     53     for(int i = first[0][s];i;i = edges[0][i].ne){
     54         e = edges[0][i];
     55         if(vis[e.to])del(e.to);
     56     }
     57 }
     58 
     59 void dijkstra(int s){
     60     priority_queue<Node> q;
     61     q.push((Node){s,0});
     62     memset(d,0x3f,sizeof(d));
     63     d[s] = 0;
     64     while(!q.empty()){
     65         u = q.top().to;
     66         q.pop();
     67         if(vis[u])continue;
     68         vis[u] = 1;
     69         for(int i = first[1][u];i;i = edges[1][i].ne){
     70             e = edges[1][i];
     71             if(d[e.to] > d[u]+1){
     72                 d[e.to] = d[u]+1;
     73                 q.push((Node){e.to,d[e.to]});
     74             }
     75         }
     76     }
     77 }
     78 
     79 int main(){
     80     n = read(),m = read();
     81     for(int i = 1;i <= m;i++){
     82         u = read(),v = read();
     83         if(u != v)add_edge(u,v);
     84     }
     85     s = read(),t = read();
     86     dfs(t);
     87     for(int i = 1;i <= n;i++)if(!vis[i]){
     88         v2[i] = 1;
     89         for(int j = first[0][i];j;j = edges[0][j].ne){
     90             e = edges[0][j];
     91             v2[e.to] = 1;
     92         }
     93     }
     94     for(int i = 1;i <= n;i++)vis[i] = v2[i];
     95     vis[s] = 0; 
     96     dijkstra(s);
     97     if(d[t] == d[0])cout << -1 << '
    ';
     98     else cout << d[t] << '
    ';
     99 return 0;
    100 }
  • 相关阅读:
    常用方法 反射常见方法
    常用方法 字符串是否是中文
    常用方法 读取 Excel的单位格 为 日期格式 的数据
    常用方法 保证数据长度相同
    常用方法 简单缓存
    P1821 [USACO07FEB]银牛派对Silver Cow Party
    P3905 道路重建
    关于宏定义
    P3512 [POI2010]PIL-Pilots
    P2398 GCD SUM
  • 原文地址:https://www.cnblogs.com/Wangsheng5/p/11525187.html
Copyright © 2011-2022 走看看