码迷,mamicode.com
首页 > 其他好文 > 详细

uva_10740_Not the best(第k短路)

时间:2015-04-18 16:11:11      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   uva   k短路   

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 vertex x to another vertex y 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 the kth (2 £ k £ 10) shortest path from x to y. If he was fortunate enough and the shortest k paths from x 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 vertex 5 to vertex 2. The shortest 7 paths from vertex 5 to vertex 2 are listed below in non-decreasing order of length. For this graph Abul was able to find the 5th shortest path which could be either 5 ? 4 ? 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 a description of the graph, source vertex x, target vertex y, and the value of k, you need to find out the length of the path Abul computed. You may assume that there exists at least one path from x 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) and m (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 of x, y and k (1 £ x, y £ 100, x 1 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 a directed edge of length l 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 the kth shortest path in the graph. If the graph does not have at least kpaths from x 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;
}


uva_10740_Not the best(第k短路)

标签:acm   algorithm   uva   k短路   

原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45113839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!