UVA10917 A walk trough the Forest

求出家到其他点的最短路径,题目的条件变成了u->v不是回头路当且仅当d[u]>d[v]。然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v])。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1001, maxm = 2002;
struct Edge
{
    int v,w,nxt;
};

#define PB push_back
vector<Edge> edges;
vector<int> G[maxn];
int head[maxn];
int d[maxn];

void addEdge(int u,int v,int w)
{
    edges.PB({v,w,head[u]});
    head[u] = edges.size()-1;
}

void init()
{
    memset(head,-1,sizeof(head));
    edges.clear();
}

typedef pair<int,int> Node;
#define fi first
#define se second
void dijkstra(int s = 1)
{
    memset(d,0x3f,sizeof(d));
    priority_queue<Node,vector<Node>,greater<Node> > q;
    q.push(Node(d[s] = 0,s));
    while(q.size()){
        Node x = q.top(); q.pop();
        int u = x.se;
        if(x.fi != d[u]) continue;
        for(int i = head[u]; ~i ; i = edges[i].nxt){
            Edge &e = edges[i];
            if(d[e.v] > d[u]+e.w){
                d[e.v] = d[u]+e.w;
                q.push(Node(d[e.v],e.v));
            }
        }
    }
}

int dp[maxn];
int dfs(int u)
{
    int &ans = dp[u];
    if(~ans) return ans;
    if(u == 1) return ans = 1;
    ans = 0;
    for(int i = 0; i < (int)G[u].size(); i++){
        ans += dfs(G[u][i]);
    }
    return ans;
}

void rebuild(int n)
{
    for(int i = 0; i < n; i++) G[i].clear();
    for(int u = 0; u < n; u++){
        for(int i = head[u]; ~i; i = edges[i].nxt){
            int v = edges[i].v;
            if(d[v] < d[u]) G[u].PB(v);
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m),n){
        init();
        while(m--){
            int u,v,w; scanf("%d%d%d",&u,&v,&w); u--;v--;
            addEdge(u,v,w); addEdge(v,u,w);
        }
        dijkstra();
        rebuild(n);
        memset(dp,-1,sizeof(dp));
        printf("%d\n",dfs(0));
    }
    return 0;
}
时间: 2024-12-23 02:22:59

UVA10917 A walk trough the Forest的相关文章

HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)

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

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

Hdoj 1428 A Walk Through the Forest 【spfa】+【记忆化搜索】

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

hdu 1142 A Walk Through the Forest (digkstra+记忆化搜索)

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

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

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

HDU1142 A Walk Through the Forest(dijkstra)

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

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

HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】

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