zoukankan      html  css  js  c++  java
  • POJ

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.

    "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."

    "Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"

    Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!

    DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.

    Input

    The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.

    The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

    Output

    A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

    Sample Input

    2 2
    1 2 5
    2 1 4
    1 2 2
    

    Sample Output

    14

    题意:

    题目大意就是告诉你有N个点,M条边(有向边),接下来M行每行三个数x,y,w,代表边x->y,距离为w。

    最后给三个数S,T,K表示问点S到T的第K短路的长度,如果没有输出 -1 。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    
    const int MAXN = 1005;
    const int INF = 0x3f3f3f3f;
    
    int N,M,S,K,T;
    
    struct Edge{
    	int to,w,next;
    }E[100*MAXN],RE[100*MAXN];//正向边,Astar用。反向边,Spfa用。 
    
    int head[MAXN],rhead[MAXN];
    int tot;
    
    struct A{
    	int f,g,id;//id是点的号。 
    	//Astar核心式子:f=g+h,这里h(i)=dis[i]。 
    	bool operator < (const A &b)const {
    		if(f == b.f)return  g > b.g;
    		return f > b.f;
    	}
    };
    
    inline void Add(int from,int to,int w){
    	E[++tot].to = to;
    	E[tot].w = w;
    	E[tot].next = head[from];
    	head[from] = tot;
    	RE[tot].to = from;
    	RE[tot].w = w;
    	RE[tot].next = rhead[to];
    	rhead[to] = tot;
    }
    
    int dis[MAXN];
    bool used[MAXN];
    
    /*求出终点到各点的最短长度dis[i]
    然后用这个长度来作为后面A*算法的启发式信息h(i)。*/
    void Spfa(int from){
    	memset(dis,INF,sizeof dis);
    	//memset(used,false,sizeof used);
    	dis[from] = 0;
    	queue<int> Q;
    	Q.push(from);
    	used[from] = true;
    	while(!Q.empty()){
    		int t = Q.front();
    		Q.pop();
    		used[t] = false;
    		for(int i=rhead[t] ; i ; i=RE[i].next){
    			if(dis[RE[i].to] > dis[t] + RE[i].w){
    				dis[RE[i].to] = dis[t] + RE[i].w;
    				if(!used[RE[i].to]){
    					used[RE[i].to] = true;
    					Q.push(RE[i].to);
    				}
    			}
    		}
    	}
    }
    
    int Astar(int from,int to){
    	int cnt = 0;
    	priority_queue<A> Q;
    	if(from == to)++K;//坑点,注意判断题意中相同点算不算最短路 
    	if(dis[from] == INF)return -1;//起点与终点不连通。
    	A t,tt;
    	t.id = from,t.g = 0,t.f = t.g + dis[from];
    	Q.push(t);
    	while(!Q.empty()){
    		tt = Q.top();
    		Q.pop();
    		if(tt.id == to){
    			++cnt;
    			if(cnt == K)return tt.g;//第K次到达时就是结果。 
    		}
    		for(int i=head[tt.id] ; i ; i=E[i].next){
    			t.id = E[i].to;
    			t.g = tt.g + E[i].w;
    			t.f = t.g + dis[t.id];
    			Q.push(t);
    		}
    	}
    	return -1;//没有第K短路 
    }
    
    inline void init(){
    	tot = 0;
    	memset(head,0,sizeof head);
    	memset(rhead,0,sizeof rhead);
    }
    
    int main(){
    	
    	while(scanf("%d %d",&N,&M) == 2){
    		init();
    		int x,y,w;
    		for(int i=1 ; i<=M ; ++i){
    			scanf("%d %d %d",&x,&y,&w);
    			Add(x,y,w);
    		}
    		scanf("%d %d %d",&S,&T,&K);
    		Spfa(T);
    		printf("%d
    ",Astar(S,T));
    	}
    	
    	return 0;
    }
  • 相关阅读:
    “中国半导体教父”张汝京:中国半导体只缺人才
    集群搭建
    Scrapy
    商品建模
    python wordcloud
    StaticFileMiddleware中间件如何处理针对文件请求
    Docker / CI / CD
    NET Memory Profiler 跟踪.net 应用内存
    SOS.dll (SOS Debugging Extension)
    Download the WDK, WinDbg, and associated tools
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514021.html
Copyright © 2011-2022 走看看