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

uva 10917

时间:2018-01-20 20:34:36      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:div   insert   printf   amp   type   ret   turn   std   clu   

求出各点与终点的距离 ,然后在求得的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

标签:div   insert   printf   amp   type   ret   turn   std   clu   

原文地址:https://www.cnblogs.com/lmjer/p/8321704.html

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