zoukankan      html  css  js  c++  java
  • 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 ",DFS(1));
         }
    return 0;
    }

  • 相关阅读:
    10.22(day11) Object类 异常
    10.19(day10)
    10.18(day9)内部类 抽象类 接口
    10.17(day8) Static关键字 包的使用 访问修饰符的权限 设计模式
    paho-mqtt error1: incorrect protocol version解决方法
    Python进阶-pickle/eval/exec
    关联分析算法Apriori和FP-Growth
    LOF聚类分析
    Python进阶-迭代器和生成器
    Linux常见坑
  • 原文地址:https://www.cnblogs.com/lmjer/p/8321704.html
Copyright © 2011-2022 走看看