G - Walk Through the Forest (UVA - 10917)

- 题目大意

一个人,他只会沿着如下条件的道路(A,B)走:存在一条从B出发回家的路径,比所有从A出发回家的路径都要短。我们的任务是要找出一共有有多少条不同的回家路径。

- 解题思路

先用dijkstra预处理出终点到每个点的最短路,然后将满足行走条件的A、B(除行走条件外,还要满足一个前提,即A、B之间要有边)用一条有向边连起来(A->B),然后利用记忆化搜索来解决这个问题。

- 代码

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int M=1e5;
const int INF=0x3f3f3f;
int d[1050],vis[1050],g[1050][1050],maps[1050];

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
                g[i][j]=INF;
        }
    }
}
void dijkstra(int s, int n) {
    memset(d, INF, sizeof(d));
    memset(vis, 0, sizeof(vis));
    d[s] = 0;
    while(1) {
        int v = -1;
        for(int u = 1; u <= n; u++) {
            if(!vis[u] && (v == -1 || d[u] < d[v]))
                v = u;
        }
        if(v == -1) break;
        vis[v] = 1;
        for(int u = 1; u <= n; u++) {
            if(!vis[u] && d[u] > d[v] + g[v][u]) {
                d[u] = d[v] + g[v][u];
            }
        }
    }
}

int dfs(int i,int n)
{
    int sum=0;
    if(i==2)
        return 1;
    if(~maps[i])
        return maps[i];
    for(int j=1;j<=n;j++)
    {
        if(g[j][i]<INF&&d[i]>d[j])
            sum+=dfs(j,n);
    }
    maps[i]=sum;
    return maps[i];
}

int main()
{
    int n,m,a,b,c;
    while(scanf("%d",&n)&&n!=0)
    {
        scanf("%d",&m);
        init(n);
       for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            g[a][b]=c;
            g[b][a]=c;
        }
      memset(maps,-1,sizeof(maps));
       dijkstra(2,n);
       printf("%d\n",dfs(1,n));
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8449669.html

时间: 2024-10-17 01:03:52

G - Walk Through the Forest (UVA - 10917)的相关文章

训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - 基础DP - Dijkstra - 图论 - 训练指南 Walk Through the Forest UVA - 10917 题意 Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比

UVA 10917 - Walk Through the Forest(dijstra)

UVA 10917 - Walk Through the Forest 题目链接 题意:公司编号为1,家编号为2,每次回家都不走回头路,回头路定义为:满足条件的道路(A,B),满足存在一条从B出发比所有从A出发的回家的路径都短,问有几种走法 思路:先从家求dijstra,这样满足条件的道路就是d[A] > d[B],这个图是一个dag,在上面进行dp就可以找出种数了 代码: #include <cstdio> #include <cstring> #include <v

UVA - 10917 Walk Through the Forest (最短路+DP)

题意:Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所有从A出发回家的路径都短,你的任务是计算有多少条不同的路径 从后往前找最短路, 对于每一步要更新之后走的位置值: #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace s

uva 10917 Walk Through the Forest(最短路)

uva 10917 Walk Through the Forest gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共有多少条不同的回家路径.其中起点的编号为1,终点的编号为2. Input 多组数据输入,每组数据第一行输入n,m(1<=n<=1000)表示点的数目和边的数目,点的编号为1~n,接下来m行每行输入3个数a,b,c表示有一条双向道路连接a,

UVa 10917 A Walk Through the Forest

A Walk Through the Forest Time Limit:1000MS  Memory Limit:65536K Total Submit:48 Accepted:15 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 t

uva 10917

Problem C: A Walk Through the Forest 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

hdu 1142 A Walk Through the Forest (最短路+dfs )

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5809    Accepted Submission(s): 2147 Problem Description Jimmy experiences a lot of stress at work these days, especial

hdu_A Walk Through the Forest ——迪杰特斯拉+dfs

A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Problem Description Jimmy experiences a lot of stress at work these days, especially since

hdu 1142 A Walk Through the Forest (最短路径)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5306    Accepted Submission(s): 1939 Problem Description Jimmy experiences a lot of stress at work these days, especiall