[UVA][第K短路] 10740 - Not the Best@Morris' Blog|PChome Online 人新台
2012-12-13 17:47:26| 人1,230| 回0 | 上一篇 | 下一篇

[UVA][第K短路] 10740 - Not the Best

0 收藏 0 0 站台

Problem A
Not the Best
Input: standard input
Output: standard output
Time Limit: 1 second

 

Abul isnot the best student in his class; neither is he the best player in his team.Not that he is bad; he is really good, but unfortunately not the best.

 

Last semesterour “not quite the best” Abul took a course on algorithms. In one of theassignments he was required to find the shortest path from a given vertex xto another vertex y in a weighted directed graph. As you have probablyalready guessed, he rarely managed to find the shortest path; instead he alwaysended up finding the kth (2 k 10)shortest path from x to y. If he was fortunate enough and theshortest k paths from x to y had the same length, he wasgiven credit for his solution.

For example, forthe graph above, Abul was asked to find the shortest path from vertex 5to vertex 2. The shortest 7 paths from vertex 5 to vertex 2are listed below in non-decreasing order of length. For this graph Abulwas able to find the 5th shortest path which could be either 54 3 2 5 1 2 or 5 1 2 5 4 3 2, each with length 15.

 

Path

Length

5 1 2

5

5 4 3 2

6

5 1 2 5 1 2

14

5 4 3 2 5 1 2

15

5 1 2 5 4 3 2

15

5 4 3 2 5 4 3 2

16

5 1 2 5 1 2 5 1 2

23

Given adescription of the graph, source vertex x, target vertex y,and the value of k, you need to find out the length of the path Abulcomputed. You may assume that there exists at least one path from xto y in the given graph.

  
Input

The input maycontain multiple test cases.

 

The first lineof each test case contains two integers n (2 n 100)and m (1 m 1000) giving respectively thenumber of vertices, and the number of edges in the graph. Each vertex in thegraph is identified by a unique integer in [1, n]. The second line ofthe test case contains the values of , y and k(1 x, y 100, x y, 2 k 10). Each of the next m lines contains three integers u,v and l (1 u,v 100, 0 l 10000) specifying adirected edge of length l from vertex u to vertex v.

 

The inputterminates with two zeros for n and m.

 

Output

For each test case in the inputoutput a line containing an integer giving the length of the kthshortest path in the graph. If the graph does not have at least k pathsfrom x to y, output a 1 instead.

 

SampleInput                              Output for Sample Input

3 3

1 3 4

1 3 3

1 2 4

2 3 5

5 6

5 2 5

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2

2 2

1 2 3

1 2 5

2 2 2

0 0

-1

15

9

 


Problemsetter: Rezaul Alam Cowdhury, University of Texas at Austin

Specialthanks: Md. Kamruzzaman, EPS


一是 第k短路,但是此不是,也正因不是也比好做,用一 heap 去即可。
於做法可以去看

zerojudge/post/1322319700

#include <stdio.h>
#include <queue>
#include <vector>
#define oo 0xffffff
using namespace std;
struct arc {
    int to, w;
    arc(int x, int y) : to(x), w(y) {}
};
vector<arc> g[105], invg[105];
int toEDdis[105], fromSTdis[105], used[105], kpath[105];
void prepare(int st, int ed, int k, int n) { // spfa
    int i, tn;
    for(i = 1; i <= n; i++)
        toEDdis[i] = oo, used[i] = 0;
    queue<int> Q;
    toEDdis[ed] = 0;
    Q.push(ed);
    while(!Q.empty()) {
        tn = Q.front();
        Q.pop();
        used[tn] = 0;
        for(i = 0; i < invg[tn].size(); i++) {
            if(toEDdis[invg[tn][i].to] > toEDdis[tn] + invg[tn][i].w) {
                toEDdis[invg[tn][i].to] = toEDdis[tn] + invg[tn][i].w;
                if(!used[invg[tn][i].to]) {
                    used[invg[tn][i].to] = 1;
                    Q.push(invg[tn][i].to);
                }
            }
        }
    }
}
struct cmp {
    bool operator()(arc a, arc b) {
        return a.w > b.w;
    }
};
void sol_kpath(int st, int ed, int k, int n) {
    prepare(st, ed, k, n);
    priority_queue<arc, vector<arc>, cmp> pQ;
    int i;
    arc tn(0,0);
    for(i = 1; i <= n; i++)
        fromSTdis[i] = oo, kpath[i] = 0;
    pQ.push(arc(st, toEDdis[st]));
    while(!pQ.empty()) {
        tn = pQ.top();
        pQ.pop();
        if(kpath[tn.to] >= k)
            continue;
        tn.w -= toEDdis[tn.to];
        kpath[tn.to]++, fromSTdis[tn.to] = tn.w;
        if(kpath[ed] == k) {
            printf("%d\n", fromSTdis[ed]);
            return;
        }
        for(i = 0; i < g[tn.to].size(); i++) {
            pQ.push(arc(g[tn.to][i].to, tn.w+g[tn.to][i].w+toEDdis[g[tn.to][i].to]));
        }
    }
    puts("-1");
}
int main() {
    int n, m, x, y, w, i, st, ed, k;
    while(scanf("%d %d", &n, &m) == 2) {
        if(!n && !m)    break;
        for(i = 1; i <= n; i++)
            g[i].clear(), invg[i].clear();
        scanf("%d %d %d", &st, &ed, &k);
        arc e(0,0);
        while(m--) {
            scanf("%d %d %d", &x, &y, &w);
            e.to = y, e.w = w;
            g[x].push_back(e);
            e.to = x;
            invg[y].push_back(e);
        }
        sol_kpath(st, ed, k, n);
    }
    return 0;
}

台: Morris
人(1,230) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][凸包] 209 - Triangular Vertices
此分上一篇:[UVA][二式定理][log] 10883 - Supermean

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86