• UVA10917_Walk Through the Forest


    无向图。对于两个相连的点,如果A到终点的最短路径大于B到终点的最短路径,那么A可以往B走,求最终从起点到终点有多少种走法?

    首先我们可以直接预处理所有点到终点的最短路径。然后分别判断所有的边两点是否满足d[U[i]]>d[V[i]],然后把把满足条件的加入到一个新图中即可。

    由于新图是一个有向无环图,那么只需要记忆话搜就可以解决问题了。

    召唤代码君:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #define maxn 1010
    #define maxm 2222222
    using namespace std;
    
    struct heapnode{
        int D,U;
        bool operator < (heapnode HP) const{
            return D>HP.D;
        }
    };
    const int inf=~0U>>2;
    int to[maxm],next[maxm],c[maxm],first[maxn],edge;
    int d[maxn],f[maxn];
    bool done[maxn];
    int n,m;
    int U[maxm],V[maxm],W[maxm];
    
    void _init()
    {
        edge=-1;
        for (int i=1; i<=n; i++) first[i]=f[i]=-1,d[i]=inf,done[i]=false;
    }
    
    void addedge(int uu,int vv,int ww)
    {
        edge++;
        to[edge]=vv,c[edge]=ww,next[edge]=first[uu],first[uu]=edge;
    }
    
    void dijkstra(int t)
    {
        priority_queue<heapnode> Q;
        Q.push((heapnode){0,t}),d[t]=0;
        while (!Q.empty())
        {
            heapnode cur=Q.top();
            Q.pop();
            int V=cur.U;
            if (done[V]) continue;
            done[V]=true;
            for (int i=first[V]; i!=-1; i=next[i])
                if (d[V]+c[i]<d[to[i]])
                    d[to[i]]=d[V]+c[i],Q.push((heapnode){d[to[i]],to[i]});
        }
    }
    
    int get(int x)
    {
        if (f[x]!=-1) return f[x];
        if (x==2) return f[x]=1;
        f[x]=0;
        for (int i=first[x]; i!=-1; i=next[i])
            f[x]+=get(to[i]);
        return f[x];
    }
    
    int main()
    {
        while (scanf("%d",&n) && n)
        {
            scanf("%d",&m);
            _init();
            for (int i=1; i<=m; i++)
            {
                scanf("%d%d%d",&U[i],&V[i],&W[i]);
                addedge(U[i],V[i],W[i]);
                addedge(V[i],U[i],W[i]);
            }
            dijkstra(2);
            edge=-1;
            for (int i=1; i<=n; i++) first[i]=-1;
            for (int i=1; i<=m; i++)
            {
                if (d[U[i]]>d[V[i]]) addedge(U[i],V[i],1);
                    else if (d[V[i]]>d[U[i]]) addedge(V[i],U[i],1);
            }
            printf("%d
    ",get(1));
        }
        return 0;
    }
  • 相关阅读:
    使用Perl5获取有道词典释义
    Compress a Folder/Directory via Perl5
    为该目录以及子目录添加index.html
    学习Perl6: slice fastq file
    Javascript Regexp match and replace
    赋值运算符函数
    扑克牌顺子
    翻转单词顺序VS左旋转字符串
    和为S的两个数字VS和为S的连续正数序列
    数组中只出现一次的数字
  • 原文地址:https://www.cnblogs.com/lochan/p/3858914.html
Copyright © 2020-2023  润新知