zoukankan      html  css  js  c++  java
  • [SPOJ 30669] Ada and Trip

    [题目链接]

              https://www.spoj.com/problems/ADATRIP/

    [算法]

           直接使用dijkstra堆优化算法即可

    [代码]

              

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e6 + 10;
    const int INF = 2e9;
    
    struct edge
    {
            int to,w,nxt;
    } e[MAXN << 1];
    
    int i,j,tot,a,b,l,n,m,u,q;
    int head[MAXN];
    pair<int,int> ans;
    
    inline void addedge(int u,int v,int w)
    {
            tot++;
            e[tot] = (edge){v,w,head[u]};
            head[u] = tot;
    }
    inline pair<int,int> dijkstra(int s)
    {
            int i,l,r,cur,ans1,ans2,v,w;
            static int dist[MAXN];
            static bool visited[MAXN];
            priority_queue< pair<int,int> > q;
            for (i = 0; i < n; i++) 
            {
                    dist[i] = INF;
                    visited[i] = false;
            }    
            q.push(make_pair(0,s));
            dist[s] = 0;
            while (!q.empty())
            {
                    cur = q.top().second;
                    q.pop();
                    if (visited[cur]) continue;
                    visited[cur] = true;
                    for (i = head[cur]; i; i = e[i].nxt)
                    {
                            v = e[i].to;
                            w = e[i].w;
                            if (dist[cur] + w < dist[v])
                            {
                                    dist[v] = dist[cur] + w;
                                    q.push(make_pair(-dist[v],v));
                            }
                    }
            }        
            ans1 = 0;
            for (i = 0; i < n; i++) 
            {
                    if (dist[i] != INF) 
                            ans1 = max(ans1,dist[i]);
            }
            ans2 = 0;
            for (i = 0; i < n; i++)
            {
                    if (dist[i] == ans1)
                            ans2++;
            }
            return make_pair(ans1,ans2);
    }
    
    int main() 
    {
            
            scanf("%d%d%d",&n,&m,&q);
            for (i = 1; i <= m; i++)
            {
                    scanf("%d%d%d",&a,&b,&l);
                    if (l == 0) continue;
                    addedge(a,b,l);
                    addedge(b,a,l);
            }
            while (q--)
            {
                    scanf("%d",&u);
                    ans = dijkstra(u);
                    printf("%d %d
    ",ans.first,ans.second);
            }
            
            return 0;
        
    }
  • 相关阅读:
    [leedcode 46] Permutations
    [leedcode 45] Jump Game II
    [leedcode 43] Multiply Strings
    [leedcode 42] Trapping Rain Water
    [leedcode 41] First Missing Positive
    [leedcode 40] Combination Sum II
    全排列
    [leedcode 39] Combination Sum
    [leedcode 38] Count and Say
    调和级数求和(分块)
  • 原文地址:https://www.cnblogs.com/evenbao/p/9389740.html
Copyright © 2011-2022 走看看