zoukankan      html  css  js  c++  java
  • HDU 1142

    A Walk Through the Forest

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4386    Accepted Submission(s): 1576

    Problem Description
    Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable. The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
     
    Input
    Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
     
    Output
    For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
     
    Sample Input

    5 6

    1 3 2

    1 4 2

    3 4 3

    1 5 12

    4 2 34

    5 2 24

    7 8

    1 3 1

    1 4 1

    3 7 1

    7 4 1

    7 5 1

    6 7 1

    5 2 1

    6 2 1

    #include<stdio.h>
    #include<iostream>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    const int Max=1111111;
    const int HH=(1<<31)-1;
    int f[1002][1002];
    __int64 val[1002];
    int dis[1002];
    bool in_queue[1002];
    int n,m;
    void bfs()
    {
        int i,x1;
        int tmp,tmp2;
        queue<int>q;
        tmp=2;
        dis[2]=0;
        q.push(tmp);
        in_queue[2]=true;
        while(q.size()>0)
        {
            tmp=q.front();
            q.pop();
            in_queue[tmp]=false;
            x1=tmp;
            for(i=1;i<=n;i++)
            {
                if(f[x1][i]!=Max && (dis[x1]==HH || dis[x1]+f[x1][i]<dis[i]) )
                {
                    dis[i]=dis[x1]+f[x1][i];
                    if(in_queue[i]==false)
                    {
                        q.push(i);
                        in_queue[i]=true;
                    }
                }
            }
        }
    }
    __int64 dfs(int x)
    {
        int i;
        if(x==2) return 1;
        if(val[x]>0) return val[x];
        for(i=1;i<=n;i++)
        {
            if(i!=x && f[x][i]!=Max && dis[i]<dis[x])
                val[x]+=dfs(i);
        }
        return val[x];
    }
    void sc()
    {
        int i,j;
        for(i=1;i<=n;i++)
        {
            printf("
    ");
            for(j=1;j<=n;j++)
                printf("%d ",f[i][j]);
        }
        printf("
    
    ");
        for(i=1;i<=n;i++)
            printf("%d ",dis[i]);
    }
    int main()
    {
        int i,j,x,y,w;
        __int64 k;
        while(scanf("%d",&n)>0)
        {
            if(n==0)break;
            scanf("%d",&m);
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    f[i][j]=Max;
            for(i=1;i<=m;i++)
            {
                scanf("%d%d%d",&x,&y,&w);
                if(w<f[x][y])
                {
                    f[y][x]=w;
                    f[x][y]=w;
                }
            }
            for(i=1;i<=n;i++)
                    dis[i]=HH;
            memset(val,0,sizeof(val));
            memset(in_queue,false,sizeof(in_queue));
            bfs();
            k=dfs(1);
            printf("%I64d
    ",k);
        //    sc();
        }
        return 0;
    }
  • 相关阅读:
    vue doubleclick 鼠标双击事件
    我是如何通过CSRF拿到Shell的
    js生成一个不重复的ID的函数的进化之路
    浅谈企业内部安全漏洞的运营(一):规范化
    如何让微信丢骰子永远只出“666”
    全能无线渗透测试工具,一个LAZY就搞定了
    关于8月31日维基解密被攻击的观察与分析
    VS2013 单元测试(使用VS2013自带的单元测试)
    解决WCF部署到IIS出现“证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限”
    VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3199706.html
Copyright © 2011-2022 走看看