• zoj 2923 Calculate Roads


    Calculate Roads

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    Little Vitta needs to go from home to school every day. On her way to school, there are n crossing and m roads in total. Because of lazy, she often gets up late in the morning. In order to get to school on time, she wishes that she can always go to school in the shortest path.(Assume that the time cost on every road is 1. And all roads are bidirectional. That is to say, if she can go from A to B, she always can go from B to A). But on some specific crossings, there are some terrible insects which makes Vitta scared. She expects the crossings which has insects don't exceed k. She wants know the number of all possible paths, can you help her?

    Input

    We assume Vitta's home is node 1, and the school is node n.

    For each case, there are 3 integers in first line, m n k(m <= 1000000, n <= 5000, k <= 50), as the problem statement says.

    In the following n lines, every line contains 2 integer x and y. y equals 1 means node x has terrible insects else if y equals 0 means there is no insect.

    In the following m lines, every line contains 2 integer p and q, which means node p and node q is linked.

    Output

    For each case, output contains only 1 line.

    If the path satisfies the requirement exists, you only need to output the numbers of paths. If not, you should output "Impossible!"

    Sample Input

    11 8 1
    1 0
    2 1
    3 0
    4 0
    5 1
    6 1
    7 0
    8 0
    1 2
    1 3
    1 4
    2 5
    2 6
    3 5
    3 7
    4 6
    5 8
    6 8
    7 8

    Sample Output

    3

    Hint To Sample

    3 possible paths are 1-3-5-8 1-4-6-8 1-3-7-8


    Tester: YANG, Kete

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #define maxn 10007
    #define MAX 2000010
    #define INF 20000000
    using namespace std;
    
    int  head[maxn] , to[MAX] , next1[MAX] ;
    int val[maxn] ,dis[maxn];
    int k , n ,num[maxn][53] ,top ;
    bool vi[maxn] ;
    int bfs( int s ,int e )
    {
        int i , j , u ,v ,ll ;
          memset(num,0,sizeof(num)) ;
          memset(vi,0,sizeof(vi)) ;
        for( i = 1 ; i <= n ;i++ )
            dis[i] = INF ;
          vi[s] = true ;
        num[s][val[s]] = 1 ;
        dis[s] = 0 ;
        queue<int>q ;
        q.push(s) ;
        while(!q.empty())
        {
            u = q.front();q.pop() ;
            for( j = 0 ; j <= k ;j++ ){
              for( i  = head[u] ; i != -1 ; i = next1[i])
            {
                v = to[i] ;
                ll = val[v]+j ;
                if(dis[v] < dis[u]+1) continue ;
                 dis[v]=dis[u]+1;
                num[v][ll] += num[u][j] ;
                if(!vi[v])q.push(v),vi[v] = true ;
            }
         }
        }
        u = 0 ;
        for( i = 0 ; i <= k;i++){
            u += num[e][i] ;
        }
        return u ;
    }
    void Unit( int u ,int v )
    {
        next1[top] = head[u] ;to[top] = v ;
        head[u] = top++ ;
    }
    int main()
    {
       int i ,m ,u,v ;
       while( scanf("%d%d%d",&m,&n,&k) != EOF )
       {
           memset(head,-1,sizeof(head)) ;
           top = 0 ;
           for( i = 1 ; i <= n ;i++)
           {
               scanf("%d%d",&u,&v) ;
               val[u] = v;
           }
           while(m--)
           {
               scanf("%d%d",&u,&v) ;
               Unit(u,v) ;
               Unit(v,u) ;
           }
           m = bfs(1,n) ;
           if(!m)
              puts( "Impossible!" );
           else
           printf("%d
    ",m);
       }
       return 0 ;
    }
    
  • 相关阅读:
    [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
    Web开发基本准则-55实录-缓存策略
    Web开发基本准则-55实录-Web访问安全
    线上Java应用排查和诊断规范
    [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序
    [慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定
    再说memcache的multiget hole(无底洞)
    RCA:未注意Curl-library Post 1024以上字节时的HTTP/1.1特性导致 HessianPHP 传输数据失败
    (研发系)职业化7个细节
    5·12和6·17两知名网站域名被劫持事件实施过程回放
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3629761.html
Copyright © 2020-2023  润新知