POJ2449 Remmarguts'Date [k 단락]

4525 단어 poj2449
Remmarguts' Date
Time Limit: 4000MS
 
Memory Limit: 65536K
Total Submissions: 21064
 
Accepted: 5736
Description
"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

처음으로 k단락 문제를 풀고 이 알고리즘을 대체적으로 정리했다.
1. 먼저 벡터가 있는 각 변을 거꾸로 배치하여 새 그림을 구성한 다음에 각 정점까지의 최단 거리(SPFA 또는 Dijkstra 모두 가능)를 구한다.
2. 구조체 유형을 정의하고 변수는 링크의 종점을 포함하며 이미 걸어온 경로 길이 g와 견적 함수 값 f, f는 우선 대기열의 맨 처음에 오름차순으로 배열한다.
3. cnt를 정의하면 현재 종점에 도착한 횟수를 나타낸다. 만약에 cnt=k가 완성되면 알고리즘이 완성되고 기점과 종점이 겹치면 +k가 되어야 한다.
#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 1002
#define maxm 100002
#define inf 0x7fffffff
using std::priority_queue;
using std::queue;

int head[maxn], Rhead[maxn]; //Rhead[] save the reverse Graph
struct Node{
	int to, dist, next;
} E[maxm], RE[maxm];
int dist[maxn]; //reverse
struct  Node2{
	int to, f, g; //           
	bool operator<(const Node2& a) const{
		return a.f < f;
	}
};
bool vis[maxn];

void addEdge(int u, int v, int d, int i)
{
	E[i].to = v; E[i].dist = d;
	E[i].next = head[u]; head[u] = i;
	RE[i].to = u; RE[i].dist = d;
	RE[i].next = Rhead[v]; Rhead[v] = i;
}

void SPFA(int s, int n, int dist[], int head[], Node E[])
{
	int i, u, v, tmp;
	for(i = 0; i <= n; ++i){
		dist[i] = inf; vis[i] = false;
	}
	u = s; vis[u] = true; dist[u] = 0;
	queue<int> Q; Q.push(s);
	while(!Q.empty()){
		u = Q.front(); Q.pop(); vis[u] = 0;
		for(i = head[u]; i != -1; i = E[i].next){
			v = E[i].to;
			tmp = dist[u] + E[i].dist;
			if(tmp < dist[v]){
				dist[v] = tmp;
				if(!vis[v]){
					vis[v] = 1;
					Q.push(v);
				}
			}
		}
	}
}

int A_star(int s, int t, int k, int n)
{
	priority_queue<Node2> Q;
	int i, u, v, cnt = 0;
	Node2 tmp, now;
	if(s == t) ++k; //       
	now.to = s; now.g = 0;
	now.f = now.g + dist[now.to];
	Q.push(now);
	while(!Q.empty()){
		now = Q.top(); Q.pop();
		if(now.to == t && ++cnt == k)
			return now.g;
		for(i = head[now.to]; i != -1; i = E[i].next){
			tmp.to = E[i].to; tmp.g = now.g + E[i].dist;
			tmp.f = tmp.g + dist[tmp.to]; Q.push(tmp);
		}
	}
	return -1;
}

int main()
{
	int n, m, s, t, k, u, v, d, i;
	while(scanf("%d%d", &n, &m) == 2){
		for(i = 0; i <= n; ++i)
			head[i] = Rhead[i] = -1;
		for(i = 0; i < m; ++i){
			scanf("%d%d%d", &u, &v, &d);
			addEdge(u, v, d, i);
		}
		scanf("%d%d%d", &s, &t, &k);
		//SPFA(s, n, dist, head, E);
		SPFA(t, n, dist, Rhead, RE);
		printf("%d
", A_star(s, t, k, n)); } return 0; }

좋은 웹페이지 즐겨찾기