uva 10917

Posted lmjer

tags:

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

求出各点与终点的距离 ,然后在求得的DAG 上做动规

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

struct my{
   int v;
   int next;
   int dist;
};

struct headnode{
  int dist;
  int u;
  bool operator <(const headnode& v)const{
     return dist>v.dist;
  }
};

typedef long long ll;
#define nil 0x7fffffff
const int maxn=100000+5;
int n,m;
int dp[maxn];
int adj[maxn+10];
bool vis[maxn+10];
int p[maxn+10];
int d[maxn+10];
my bian[maxn*2+10];
int fa;

void init(){
   memset(adj,-1,sizeof(adj));
   memset(bian,-1,sizeof(bian));
   memset(p,0,sizeof(p));
   memset(vis,false,sizeof(vis));
   memset(dp,0,sizeof(dp));
   fa=0;
}

void myinsert(int u,int v,int dist){
  bian[++fa].v=v;
  bian[fa].next=adj[u];
  bian[fa].dist=dist;
  adj[u]=fa;
}

void dijkstra(int x){
     priority_queue<headnode>q;
     headnode c;
     for (int i=0;i<=n;i++) d[i]=nil;
     d[x]=0;
     c.u=x;
     c.dist=0;
     q.push(c);
     while(!q.empty()){
        c=q.top();
        q.pop();
        int u=c.u;
        if(vis[u]) continue;
        vis[u]=true;
        for (int i=adj[u];i!=-1;i=bian[i].next){
            my v=bian[i];
            if(d[u]+v.dist<d[v.v]){
                d[v.v]=d[u]+v.dist;
                p[v.v]=u;
                if(vis[v.v]) continue;
                c.u=v.v;
                c.dist=v.dist;
                q.push(c);
            }
        }
        vis[u]=false;
     }
}
int DFS(int pos) {
    if (pos == 2) return 1;
    if (dp[pos] > 0) {
        return dp[pos];
    }
    int ans = 0;
    for (int i=adj[pos];i!=-1;i=bian[i].next) {
        if (d[pos] > d[bian[i].v]) {
            ans+=DFS(bian[i].v);
        }
    }
    return dp[pos]=ans;
}

int main(){
    int u,v,zhi;
     while(scanf("%d",&n)&&n!=0){
        scanf("%d",&m);
        init();
        for (int i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&zhi);
            myinsert(u,v,zhi);
            myinsert(v,u,zhi);
        }
        dijkstra(2);
       printf("%d\n",DFS(1));
     }
return 0;
}

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

UVa 10917 林中漫步

Uva10917 Walk Through the Forest

UVA10917 Walk Through the Forest

Uva 10917 Walk Through the Forest

UVA10917 Walk Through the Forest

UVA 10917