Uva 10917 Walk Through the Forest

Posted 晓风微微

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Uva 10917 Walk Through the Forest相关的知识,希望对你有一定的参考价值。

先求出,终点到各个点的最短路,然后根据距离建立一个新图,转化为DAG动态规划问题。

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<queue>
#define MAXN 2010
#include<algorithm>
typedef long long LL;
struct teamdata{
    LL d;LL point;
    bool operator<(const teamdata& pt)const{
        return this->d>pt.d;
    }
};
struct bian{
    LL d,point;
    bool operator<(const bian &pt){
        return this->d<pt.d;
    }
};
LL n,m,k,l,s,t;
LL pre[MAXN];
vector<LL> ansl;
vector<bian> maps[MAXN];
const LL inf=0x7fffffff;
LL d[MAXN];
priority_queue<teamdata> team;
bool v[MAXN];
LL SPFA(LL s){
    LL ans=-1;
    teamdata pt;pt.d=0;pt.point=s;
    memset(v,0,sizeof(v));
    while(!team.empty()) team.pop();
    team.push(pt);teamdata pd;
    for (LL i=0;i<=MAXN-1;i++) d[i]=inf,pre[i]=-1,v[i]=0;
    d[s]=0;v[s]=1;
    while(!team.empty()){
        pt=team.top();team.pop();
        //if (pt.d>d[pt.point]) continue;
        if (pt.point==t){
            ans=pt.d;continue; 
        } 
        v[pt.point]=1;
        for (LL i=0;i<maps[pt.point].size();i++){
            LL nb=maps[pt.point][i].point;
            if (v[nb]) continue;
            if (d[nb]>pt.d+maps[pt.point][i].d){
                d[nb]=pt.d+maps[pt.point][i].d;
                pre[nb]=pt.point;
                pd.d=d[nb];pd.point=nb;team.push(pd);
            }
        }
    }
    return ans;
}
int aa[MAXN][MAXN];
LL f[MAXN];
bian fh[MAXN];
bool a[MAXN][MAXN];
LL dfs(int x){
    if (f[x]>-1) return f[x];
    LL ans=0;
    for (int i=1;i<=n;i++){
    //    if (i==x) continue;
        if (aa[x][i]) ans+=dfs(i);    
    }
    f[x]=ans;
    return f[x];
}
LL deal(){
    if (scanf("%lld",&n)==EOF||n==0) return 0;
    scanf("%lld",&m);
    for (int i=0;i<=n;i++){
        for (int j=0;j<=n;j++)
        {
            a[i][j]=0;f[i]=-1;
        }
    }
    for (LL i=0;i<=n;i++) maps[i].clear();
    for (LL i=0;i<m;i++){
        LL p,q,w;
        scanf("%lld%lld%lld",&p,&q,&w);
        bian pt;
        a[p][q]=a[q][p]=w;
        pt.point=q;pt.d=w;
        maps[p].push_back(pt);
        pt.point=p;
        maps[q].push_back(pt);
    }
    SPFA(2);
    memset(aa,0,sizeof(aa));
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            if (a[i][j]&&d[i]>d[j]) aa[i][j]=1;
        }
    }
    f[2]=1;
    printf("%lld\n",dfs(1));
}
int main(){
    while(deal());         
    return 0;
}

 

以上是关于Uva 10917 Walk Through the Forest的主要内容,如果未能解决你的问题,请参考以下文章

UVA10917 Walk Through the Forest

Uva10917 Walk Through the Forest

UVA10917 Walk Through the Forest

UVA10917 Walk Through the Forest

G - Walk Through the Forest (UVA - 10917)

UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)