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;
    }
  • 相关阅读:
    Selenium2+python自动化-使用JS脚本处理网页滚动条
    Python定时框架 Apscheduler 详解【转】
    转:VMware 15 安装 MAC OS 10.13 原版(详细图文教程)
    Appium+Robotframework实现iOS应用的自动化测试
    在jenkins打开roboframework报告:Opening Robot Framework report failed
    springcloud使用pagehelper 实现分页,及total 数据问题
    日志打印request 和response 内容
    springboot+elasticsearch + rabbitMQ实现全文检索(使用transportClient 实现CRUD)
    springboot+elasticsearch + rabbitMQ实现全文检索(springboot+ES整合)
    springboot+elasticsearch + rabbitMQ实现全文检索(项目搭建)
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3199706.html
Copyright © 2011-2022 走看看