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

Uva 10917 Walk Through the Forest

时间:2017-10-02 18:54:01      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ffffff   转化   sizeof   wal   queue   str   inf   data   this   

先求出,终点到各个点的最短路,然后根据距离建立一个新图,转化为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

标签:ffffff   转化   sizeof   wal   queue   str   inf   data   this   

原文地址:http://www.cnblogs.com/xfww/p/7620544.html

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