24h購物| | PChome| 登入
2013-05-27 20:50:09| 人氣1,180| 回應0 | 上一篇 | 下一篇

[UVA][A*][次短路徑] 10342 - Always Late

推薦 0 收藏 0 轉貼0 訂閱站台

Always Late

Ajmat: Have you noticed one member of team 13 comes late in almost every contest?
Nejhum:   You mean the team of Mahbub, Moazzem and Yeamin? What's the problem with them?
Ajmat: All of them lives far away from BUET, and they always avoid the shortest path to BUET.
Nejhum: Are you kidding? Why would they do so? A computer science student won't ever do that.
Ajmat: They always avoids the shortest path. They believe that they always face the worst traffic jam on their shortest way to BUET.
Nejhum: That sounds crazy!
Ajmat: They are more crazy than you think they are. They always try to use the path whose length is smaller than or equal to that of any path to BUET except the shortest one. And for that, they sometimes visit the same junction more than once.
Nejhum: But they are not using the longest path. Then why are they late almost everyday?
Ajmat: That's because they have to spend a lot of time to find out which of the paths meet their criteria. Perhaps they don't know how to find out the second-shortest path.
Nejhum: I think we should solve this real problem in today's contest, instead of solving imaginary ones.
Ajmat: Let's try.

Input

The first line of each test case contains two integers: n (the number of junctions, 1 < n < 100) and r (the number of bi-directional roads connecting these junctions). The junctions are numbered with 0, 1, ..., n-1. Each of the next r lines (one for each road) contains three integers: two junction-numbers that the corresponding road connects and the length of the road in kilometers. The next line contains an integer q, the number of queries. Each of the next q lines contains two junction-numbers. There is a blank line after the q queries. There is at most 1 road between each junction pair. A road always connects two different junctions. Length of a road is not less than 1km and not more than 100km.
 
Input is terminated by EOF.
 

Output

For each set of output, print the set # (1, 2, � ) followed by q lines, one for each query, each containing the length of the second-shortest path between the corresponding junctions. However, for the unsolvable queries, print a '?'.
 

Sample Input

4 3
0 1 12
0 2 20
1 2 15
3
0 1
0 2
0 3

4 3
0 1 11
0 2 20
1 2 15
3
0 1
0 2
0 3

Sample Output

Set #1
35
27
?
Set #2
33
26
?

Mustaq Ahmed. 12-07-2002.

求次短路徑,同一條邊可重複,但是不可以一樣長。

解法:A* Algorithm

操作步驟如下鏈結

http://mypaper.pchome.com.tw/zerojudge/post/1322319700


#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct edge {
    int to, v;
    edge(int a, int b):
        to(a), v(b) {}
};
vector<edge> g[105];
#define oo 0xfffffff
void spfa(int st, int n, int dis[]) {
    int i, tn;
    int used[105] = {};
    queue<int> Q;
    for(i = 0; i < n; i++)
        dis[i] = oo;
    dis[st] = 0;
    Q.push(st);
    while(!Q.empty()) {
        tn = Q.front(), Q.pop();
        used[tn] = 0;
        for(vector<edge>::iterator it = g[tn].begin();
            it != g[tn].end(); it++) {
            if(dis[it->to] > dis[tn] + it->v) {
                dis[it->to] = dis[tn] + it->v;
                if(used[it->to] == 0) {
                    used[it->to] = 1;
                    Q.push(it->to);
                }
            }
        }
    }
}
struct E {
    int node, g, h;
    E(int a, int b, int c):
        node(a), g(b), h(c) {}
    bool operator<(const E &a) const {
        return g + h > a.g + a.h;
    }
};
void sol_kpath(int st, int ed, int k, int n) {
    int H[105];
    int cntkpath[105], kpath[105];
    int i, j;
    spfa(ed, n, H);
    for(i = 0; i < n; i++)
        cntkpath[i] = 0, kpath[i] = -1;
    cntkpath[st] = 0; // st->st 0
    kpath[st] = -1;
    priority_queue<E, vector<E> > pQ;
    E tn(0,0,0);
    pQ.push(E(st,0,H[st]));
    while(!pQ.empty()) {
        tn = pQ.top(), pQ.pop();
        //printf("%d %d %d\n", tn.node, tn.g, tn.h);
        if(cntkpath[tn.node] >= k ||
           kpath[tn.node] == tn.g+tn.h)
           continue;
        cntkpath[tn.node]++, kpath[tn.node] = tn.g+tn.h;
        if(tn.node == ed && cntkpath[tn.node] == k) {
            printf("%d\n", tn.g);
            return;
        }
        for(vector<edge>::iterator it = g[tn.node].begin();
            it != g[tn.node].end(); it++) {
            pQ.push(E(it->to, tn.g + it->v, H[it->to]));
        }
    }
    puts("?");
}
int main() {
    int n, m, q, cases = 0;
    int x, y, v, i;
    while(scanf("%d %d", &n, &m) == 2) {
        for(i = 0; i < n; i++)
            g[i].clear();
        while(m--) {
            scanf("%d %d %d", &x, &y, &v);
            g[x].push_back(edge(y, v));
            g[y].push_back(edge(x, v));
        }
        printf("Set #%d\n", ++cases);
        scanf("%d", &q);
        while(q--) {
            scanf("%d %d", &x, &y);
            sol_kpath(x, y, 2, n);
        }
    }
    return 0;
}

台長: Morris
人氣(1,180) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][spfa][次短路徑]
此分類上一篇:[UVA][math] 12546 - LCM Pair Sum

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文