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): 5999 Accepted Submission(s): 2215


    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 0



    Sample Output

    2 4



    Source

    University of Waterloo Local Contest 2005.09.24

    Dijkstra

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    int n,m;
    const int maxn=2010;
    const int inf=0x3ffffff;
    int cost[maxn][maxn];
    int num[maxn];
    bool uesd[maxn];
    int d[maxn];
    void dijkstra(int s)
    {
         for(int i=0;i<=n;i++)
         {
             d[i]=inf;
             uesd[i]=0;
         }
         d[s]=0;
         while(1){
            int v=-1;
            for(int i=1;i<=n;i++)
            {
                if(!uesd[i]&&(v==-1||d[i]<d[v])) {v=i;}
            }
            uesd[v]=1;
            if(v==-1) break;
            for(int i=1;i<=n;i++)
            {
                if(d[i]>d[v]+cost[i][v]) {d[i]=d[v]+cost[i][v];}
            }
         }
    }
    int bfs(int s)
    {
        if(num[s]) return num[s];
        if(s==2) return 1;
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(cost[s][i]<inf&&d[s]>d[i]){
                if(num[i]) sum=sum+num[i];
                else sum=sum+bfs(i);
            }
        }
        sum=sum+num[s];
        num[s]=sum;
        return num[s];
    }
    int main()
    {
        int a,b,c;
        while(1){
                scanf("%d",&n);
            if(!n) break;
            for(int i=0;i<=n;i++)
               for(int j=0;j<=n;j++)
                cost[i][j]=inf;
           for(int i=0;i<=n;i++) cost[i][i]=0;
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                cost[a][b]=cost[b][a]=c;
            }
            dijkstra(2);
            memset(num,0,sizeof(num));
             printf("%d
    ",bfs(1));
        }



    Heap+Dijkstra

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    int n,m;
    const int maxn=2007;
    const int inf=0x3fffffff;
    bool flag[maxn][maxn];
    int num[maxn];
    struct HeapNode
    {
        int d,u;
        HeapNode(int dd,int uu):d(dd),u(uu){}
        bool operator < (const HeapNode& rhs) const{
           return d>rhs.d;
           }
    };
    struct Edge
    {
        int from,to,dist;
        Edge(int u,int v,int d):from(u),to(v),dist(d){}
    };
    vector<Edge> edges;
    vector<int> G[maxn];
    bool done[maxn];
    int d[maxn],p[maxn];
    void init(){
      for(int i=0;i<=n;i++) G[i].clear();
      edges.clear();
    }
    void AddEdge(int from,int to,int dist)
    {
        int mm;
        edges.push_back(Edge(from,to,dist));
        mm=edges.size();
        G[from].push_back(mm-1);
    }
    void dijkstra(int s)
    {
         priority_queue<HeapNode> Q;
         for(int i=0;i<=n;i++) d[i]=inf;
         d[s]=0;
         memset(done,0,sizeof(done));
         Q.push(HeapNode(0,s));
         while(!Q.empty()){
            HeapNode x=Q.top();Q.pop();
            int u=x.u;
            if(done[u]) continue;
            done[u]=true;
            for(int i=0;i<G[u].size();i++){
                Edge& e=edges[G[u][i]];
                if(d[e.to]>d[u]+e.dist){
                    d[e.to]=d[u]+e.dist;
                    p[e.to]=G[u][i];
                    Q.push(HeapNode(d[e.to],e.to));
                }
            }
         }
    }
    int bfs(int s)
    {
        if(num[s]) return num[s];
        if(s==2) return 1;
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(flag[s][i]&&d[s]>d[i]){
                if(num[i]) sum=sum+num[i];
                else sum=sum+bfs(i);
            }
        }
        sum=sum+num[s];
        num[s]=sum;
        return num[s];
    }
    int main()
    {
        int a,b,c;
        while(1){
            memset(flag,0,sizeof(flag));
            scanf("%d",&n);
            init();
            if(!n) break;
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                AddEdge(a,b,c);
                AddEdge(b,a,c);
               flag[a][b]=flag[b][a]=1;
            }
            dijkstra(2);
            memset(num,0,sizeof(num));
            int ans=0;
             int  sum=d[1];
             for(int i=3;i<=n;i++)
             {
                 if(d[i]<sum) ans++;
             }
             printf("%d
    ",bfs(1));
        }
    }



  • 相关阅读:
    运算符的优先级(从高到低)
    常用字符与ASCII代码对照表
    02.数据类型常量与变量
    Java基础01
    2以太坊入门的方法2
    区块链学习笔记1
    5关键字this与super的区别
    4Java中成员变量与局部变量
    lua返回页面时中文乱码
    struts2中<jsp:forward>跳转时报404错误的问题
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254380.html
Copyright © 2011-2022 走看看