码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1142 A Walk Through the Forest

时间:2016-07-24 13:30:49      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

A Walk Through the Forest

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

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

思路:同 hdu 1428 校园漫步
先求出bfs,每点到终点的距离(为后面能不能走作判断基础),再统计,路线数量
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

const int inf=0x7fffffff;
const int maxn=1005;
int n,m,i,j;
long long  f[maxn],dis[maxn];
int st[maxn];

struct node
{
    int num,d;
    node(int a,int b){num=a; d=b;}
};
vector<node> s[maxn];

bool cmp(int  a,int  b)
{
    return dis[a]>dis[b];
}

void bfs()
{
    queue<int> Q;
    Q.push(2);
   // memset(vis,0,sizeof(vis));
    dis[2]=0;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for(int i=0;i<s[u].size();i++)
        {
            if(dis[u]+s[u][i].d>=dis[s[u][i].num]) continue;
            dis[s[u][i].num]=dis[u]+s[u][i].d;
            Q.push(s[u][i].num);
            //不能用vis[]数组判断是否访问过。
            //原因:如果被访问过,不一定在队列中,就不能更新其他点。
        }
    }
    return;
}
int main()
{
    while(scanf("%d",&n))
    {
        if (n==0) break;
        scanf("%d",&m);
        for(i=1;i<=n;i++)
        {
            s[i].clear();
            dis[i]=inf;
        }
        for(i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            s[x].push_back(node(y,z));
            s[y].push_back(node(x,z));
        }
        bfs();
        for(i=1;i<=n;i++) st[i]=i;
        sort(st+1,st+n+1,cmp);
        memset(f,0,sizeof(f));
        f[1]=1;
        for(i=1;i<=n;i++)
        {
            for(j=0;j<s[st[i]].size();j++)
             if (dis[ s[st[i]][j].num ]>dis[st[i]])
                 f[st[i]]+=f[s[st[i]][j].num];
        }
        printf("%lld\n",f[2]);
    }
    return 0;
}

 

HDU 1142 A Walk Through the Forest

标签:

原文地址:http://www.cnblogs.com/stepping/p/5700570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!