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;
}

posted @ 2018-01-20 19:24  lmjer  阅读(99)  评论(0编辑  收藏  举报