zoukankan      html  css  js  c++  java
  • POJ 3255

    题意: 次短路,

    裸的板子;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    const int maxn = 1e5 +131;
    const int INF = 0x7fffffff;
    struct Edge{
        int to, cost;
        Edge(int a, int b) : to(a), cost(b) {}
    };
    
    vector<Edge> G[maxn];
    void AddEdge(int from, int to, int cost) {
        G[from].push_back(Edge(to, cost));
        G[to].push_back(Edge(from, cost));
    }
    
    typedef pair<int, int> P;
    int N, R;
    int Dist[maxn], Dist2[maxn];
    
    int Solve()
    {
        priority_queue<P, vector<P>, greater<P> > que;
        fill(Dist, Dist + N, INF);
        fill(Dist2, Dist2 + N, INF);
        Dist[0] = 0;
        que.push(P(0,0));
    
        while(!que.empty())
        {
            P tmp = que.top(); que.pop();
            int v = tmp.second, d = tmp.first;
            if(Dist2[v] < d) continue;
            for(int i = 0; i < G[v].size(); ++i)
            {
                Edge &e = G[v][i];
                int d2 = d + e.cost;
                if(Dist[e.to] > d2) {
                    swap(Dist[e.to], d2);
                    que.push(P(Dist[e.to], e.to));
                }
                if(Dist2[e.to] > d2 && Dist[e.to] < d2)
                {
                    Dist2[e.to] = d2;
                    que.push(P(Dist2[e.to], e.to));
                }
            }
        }
        return Dist2[N-1];
    }
    
    int main()
    {
        while(scanf("%d%d", &N, &R)!=EOF)
        {
            if(N == 0 && R == 0) break;
            for(int i = 0; i <= N; ++i) G[i].clear();
            for(int i = 0; i < R; ++i)
            {
                int u, v, c;
                scanf("%d%d%d", &u, &v, &c);
                AddEdge(u-1,v-1,c);
            }
            printf("%d
    ",Solve());
        }
        return 0;
    }
    
  • 相关阅读:
    self 和 super 关键字
    NSString类
    函数和对象方法的区别
    求两个数是否互质及最大公约数
    TJU Problem 1644 Reverse Text
    TJU Problem 2520 Quicksum
    TJU Problem 2101 Bullseye
    TJU Problem 2548 Celebrity jeopardy
    poj 2586 Y2K Accounting Bug
    poj 2109 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/aoxuets/p/5506856.html
Copyright © 2011-2022 走看看