1. 程式人生 > >POJ3255(次短路)

POJ3255(次短路)

題意:第一行是點數N和邊數R,後面是R條無向邊。求從1到n的次短路長度。

思路:模板題。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <cstdlib>
#include <set>
#include <string>

using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 5005;
struct edge{
    int v, w, nxt;
}G[200005]; // 無向邊陣列要開兩倍
int tot, pre[maxn];
void add(int u, int v, int w) {
    G[tot].v = v;
    G[tot].w = w;
    G[tot].nxt = pre[u];
    pre[u] = tot++;
}
int dist[maxn][2], n, r;
bool vis[maxn][2];
struct node{
    int v, d, flag;
    node() {}
    node(int v, int d, int f) : v(v), d(d), flag(f) {}
    bool operator < (const node &b) const {
        if (d != b.d) {
            return d > b.d;
        } else {
            return v > b.v;
        }
    }
};
void dijkstra(int s) {
    memset(dist, 0x3f, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    dist[s][0] = 0;
    priority_queue<node> que;
    que.push(node(s, 0, 0));
    while (!que.empty()) {
        node p = que.top();
        que.pop();
        if (vis[p.v][p.flag]) {
            continue;
        }
        vis[p.v][p.flag] = true;
        for (int i = pre[p.v]; ~i; i = G[i].nxt) {
            int v = G[i].v;
            int w = G[i].w;
            if (!vis[v][0] && p.d + w < dist[v][0]) {
                if (dist[v][0] != inf) {
                    dist[v][1] = dist[v][0];
                    que.push(node(v, dist[v][1], 1));
                }
                dist[v][0] = p.d + w;
                que.push(node(v, dist[v][0], 0));
            } else if (!vis[v][1] && p.d + w < dist[v][1]) {
                dist[v][1] = p.d + w;
                que.push(node(v, dist[v][1], 1));
            }
        }
    }
}
int main(){
    int u, v, w;
    scanf("%d%d", &n, &r);
    tot = 0;
    memset(pre, -1, sizeof(pre));
    while (r--) {
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w);
        add(v, u, w);
    }
    dijkstra(1);
    printf("%d\n", dist[n][1]);
    return 0;
}

相關推薦

POJ3255短路

題意:第一行是點數N和邊數R,後面是R條無向邊。求從1到n的次短路長度。 思路:模板題。 #include <iostream> #include <cstdio> #include <cstring> #include <alg

洛谷P2865 [USACO06NOV]路障Roadblocks短路

題目描述 貝茜把家搬到了一個小農場,但她常常回到FJ的農場去拜訪她的朋友。貝茜很喜歡路邊的風景,不想那麼快地結束她的旅途,於是她每次回農場,都會選擇第二短的路徑,而不象我們所習慣的那樣,選擇最短路。 貝茜所在的鄉村有RRR(1&lt;=R&lt;

poj3463短路

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way,

USACO2007NOV——短路徑Roadblocks短路

描述 貝西所在的牧場一共有 N 個地點。 M 條雙向通行的道路連線這些地點,其中第 i 條道路連線 Ai 和 Bi,長度為 Li。貝西想從第一個地點走到第 N 個地點,由於路上風景不錯,她決定不走最短 路徑,而選擇次短路徑。次短路徑的長度嚴格大於最短路徑。如果

hdu1688 Sightseeing短路

題目連結: 題意: 給你n個點,m條有向邊,問你從s走到t點最短路加上最短路權值加一的路徑條數 資料範圍: 2≤n≤1000,1≤m≤10000,1≤s,t≤n,s≠t,且沒有自環 題解: 我們相當於求最短路和次短路,注意這裡的次短路是權

POJ3255---求短路用Dijkstra

題目連結 分析:百度翻譯一下題目發現是道求最短路且可以重複經過某點的情況,可以用Dijkstra求,具體做法:用 dist1[] 記錄最短路,dist2[] 記錄次短路。然後還是 spfa, 更新的時候分情況判斷一下,次短路可能更新次短路,最短路可能更新次短路和最短路。。

[POJ3463] Sightseeing短路 Heap + Dijkstra

opened dig [0 pan http con 比較 etc priority 傳送門 用dijkstra比較好,spfa可能有的重復 dis[x][2]:dis[x][0]表示起點到x的最短路、dis[x][1]表示起點到x的次短路; tot[x][2]

poj 3255 Roadblocks 短路 A星演算法

題目連結:http://poj.org/problem?id=3255 Language:Default Roadblocks Time Limit: 2000MS

poj 3463 Sightseeing短路短路

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way,

POJ 3255 Roadblocks短路模板題

http://poj.org/problem?id=3255 寫了還算多的最短路題目發現,沒寫過次短路。順著挑戰刷,就看到了次短路,然後發現一臉懵逼。 其實理解了後發現超級簡單。次短路,每個點都記錄兩個距離就好了,最短的和次短的。然後最後輸出第n個點的次短的即可。 程式碼如

HDU 6181Two Paths——————短路

Two Paths Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 2139&nb

Poj 3255Dijkstra求短路

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, be

poj 3463 Sightseeing短路&&短路

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way,

POJ 3255短路+SPFA

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, be

poj 3255 Roadblocks 短路 A星演算法

Language:Default Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20256 Accepted: 7107

【POJ - 3255】Roadblocks短路 Dijkstra演算法

Roadblocks 直接翻譯了 Descriptions Bessie搬到了一個新的農場,有時候他會回去看他的老朋友。但是他不想很快的回去,他喜歡欣賞沿途的風景,所以他會選擇次短路,因為她知道一定有一條次短路。這個鄉村有R(1<=R<=100000)條雙向道路,每一條連線N(1<=N

網絡提速短路

三臺 oid size 註意 減少 組成 getchar() cst etc codevs——1243 網絡提速 時間限制: 1 s 空間限制: 128000 KB 題目等級 : 黃金 Gold

單源最短路徑短路

ext getchar 路徑 鄰接鏈表 單源最短路 fin struct true com 洛谷——P3371 【模板】單源最短路徑(spfa) 題目描述 如題,給出一個有向圖,請輸出從某一點出發到所有點的最短路徑長度。 輸入輸出格式 輸入格

Currency Exchange短路

quest ber its 只需要 nging lars script end ive                            poj—— 1860 Currency Exchange Time Limit: 1000

短路第七屆福建省大學生程序設計競賽 Problem J- X

main tails return ros code and rect list def Problem Description X is a fully prosperous country, especially known for its complicated