uva_10740_Not the best(第k短路)

来源:互联网 发布:郑州php机构 编辑:程序博客网 时间:2024/06/06 04:39

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

 

Abul is not 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 semester our “not quite the best” Abul took a course on algorithms. In one of the assignments he was required to find the shortest path from a given vertexx to another vertexy in a weighted directed graph. As you have probably already guessed, he rarely managed to find the shortest path; instead he always ended up finding thekth (2 £ k £ 10) shortest path fromx toy. If he was fortunate enough and the shortestk paths fromx to y had the same length, he was given credit for his solution.

For example, for the graph above, Abul was asked to find the shortest path from vertex5 to vertex2. The shortest 7 paths from vertex5 to vertex2 are listed below in non-decreasing order of length. For this graphAbul was able to find the5th shortest path which could be either5 ® 4 ® 3 ® 2 ® 5 ® 1 ® 2 or5 ® 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 a description of the graph, source vertex x, target vertexy, and the value ofk, you need to find out the length of the pathAbul computed. You may assume that there exists at least one path fromx to y in the given graph.

  
Input

The input may contain multiple test cases.

 

The first line of each test case contains two integers n (2 £ n £ 100) andm (1 £ m £ 1000) giving respectively the number of vertices, and the number of edges in the graph. Each vertex in the graph is identified by a unique integer in[1, n]. The second line of the test case contains the values ofx, y and k (1 £ x, y £ 100, x ¹ y, 2 £ k £ 10). Each of the nextm lines contains three integersu, v andl(1 £u, v£ 100, 0 £l£ 10000)specifying a directed edge of lengthl from vertex uto vertex v.

 

The input terminates with two zeros for n and m.

 

Output

For each test case in the input output a line containing an integer giving the length of thekth shortest path in the graph. If the graph does not have at leastkpaths fromx to y, output a –1 instead.


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


Output for Sample Input

-1

15

9


题意:给你一个有向图,求出从起点到终点的第K短路。

分析:A*算法求解第K短路。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=38669

代码清单:

#include<map>#include<cmath>#include<cctype>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int maxn = 1000 + 5;const int maxv = 100 + 5;const int max_dis = 1e9;struct Edge{    int to,dis;    Edge(int to,int dis){        this -> to = to;        this -> dis = dis;    }};struct Node{    int the;    int g,h;    friend bool operator<(Node x,Node y){        return x.g+x.h>y.g+y.h;    }};int N,M;int a,b,c;int s,t,k;int dist[maxv];vector<Edge>G1[maxv];vector<Edge>G2[maxv];typedef pair<int,int>P;void dijkstra(int s){    fill(dist+1,dist+1+N,max_dis);    priority_queue<P,vector<P>,greater<P> >q;    while(q.size()) q.pop();    dist[s]=0;    q.push(P(0,s));    while(q.size()){        P p=q.top(); q.pop();        int v=p.second;        if(dist[v]<p.first) continue;        for(int i=0;i<G2[v].size();i++){            Edge& e=G2[v][i];            if(dist[e.to]>dist[v]+e.dis){                dist[e.to]=dist[v]+e.dis;                q.push(P(dist[e.to],e.to));            }        }    }}int A_star(int s,int t,int k){    if(s==t) k++;    if(dist[s]==max_dis) return -1;    priority_queue<Node>Q;    while(Q.size()) Q.pop();    Node u,w;    int cnt=0;    u.the=s; u.g=0; u.h=dist[s];    Q.push(u);    while(Q.size()){        u=Q.top(); Q.pop();        int v=u.the;        if(v==t) cnt++;        if(cnt==k) return u.g;        for(int i=0;i<G1[v].size();i++){            Edge& e=G1[v][i];            w.the=e.to;            w.g=u.g+e.dis;            w.h=dist[e.to];            Q.push(w);        }    }    return -1;}int main(){    while(scanf("%d%d",&N,&M)!=EOF){        if(N==0&&M==0) break;        for(int i=0;i<maxv;i++){            G1[i].clear();            G2[i].clear();        }        scanf("%d%d%d",&s,&t,&k);        for(int  i=0;i<M;i++){            scanf("%d%d%d",&a,&b,&c);            G1[a].push_back(Edge(b,c));            G2[b].push_back(Edge(a,c));        }        dijkstra(t);        printf("%d\n",A_star(s,t,k));    }    return 0;}


0 0