zoukankan      html  css  js  c++  java
  • P1608 路径统计

    \(dijkstra\)最短路径计数,注意判输入时的重复数据(=_=)

    const int N=2010;
    struct Node
    {
        int a,b,c;
        bool operator<(const Node &W) const
        {
            if(a == W.a)
            {
                if(b == W.b)
                    return c<W.c;
                else return b<W.b;
            }
            else return a<W.a;
        }
    };
    set<Node> S;
    vector<PII> g[N];
    int dist[N];
    bool vis[N];
    int f[N];
    int n,m;
    
    void dijkstra()
    {
        memset(dist,0x3f,sizeof dist);
        priority_queue<PII,vector<PII>,greater<PII> > heap;
        dist[1]=0;
        heap.push({0,1});
        f[1]=1;
    
        while(heap.size())
        {
            int t=heap.top().se;
            heap.pop();
    
            if(vis[t]) continue;
            vis[t]=true;
    
            for(int i=0;i<g[t].size();i++)
            {
                int j=g[t][i].fi,w=g[t][i].se;
                if(dist[j] > dist[t]+w)
                {
                    dist[j]=dist[t]+w;
                    f[j]=f[t];
                    heap.push({dist[j],j});
                }
                else if(dist[j] == dist[t]+w)
                {
                    f[j]+=f[t];
                }
            }
        }
    }
    
    int main()
    {
        ios;
        cin>>n>>m;
    
        while(m--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            if(S.count({a,b,c})) continue;
            S.insert({a,b,c});
            g[a].pb({b,c});
        }
    
        dijkstra();
    
        if(dist[n] == INF) cout<<"No answer"<<endl;
        else cout<<dist[n]<<' '<<f[n]<<endl;
    
        //system("pause");
    }
    
  • 相关阅读:
    Android theme应用主题实现
    CSS_复习
    复习_数字与字符串
    网络编程
    JAVA--多线程
    File类
    c#中new hisase关键字
    内存泄漏、程序崩溃的排除办法
    解决跨线程问题的几种办法
    c#接口
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14028923.html
Copyright © 2011-2022 走看看