HDU2449 Remmarguts'Date(제 k 단락)

4990 단어 도론
Remmarguts' Date
Time Limit: 4000MS
 
Memory Limit: 65536K
Total Submissions: 24007
 
Accepted: 6535
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단락을 구하세요.
문제 해결 방법:
spfa를 이용하여 한 번의 최단로를 역방향으로 구하면 종점(역방향으로 변하면 기점)에서 각 점까지의 최단 경로를 구할 수 있고 A*의 평가 함수로 변한다. A*의 방정식은 f(v)=g(v)+h(v)이다. 그 중에서 f는 평가 함수이고 g(v)는 출발점에서 이 점까지의 거리이며 h(v)는 예처리된 거리이다. 즉, 방금 spfa가 역방향으로 구한 기점에서 각 점까지의 최단거리 값이다.
AC 코드:
#include
#include
#include 
#include
using namespace std;

const int maxe = 100000;
const int inf = 0x3f3f3f3f;
struct Node1
{
	int to;
	int g,f; // f = g + h;
	bool operator < (const Node1 &r) const
	{
		if(r.f == f) return r.g < g;
		return r.f < f;	
	}	
};

int dist[maxe];
int head[maxe];
int ehead[maxe];
int outque[maxe];

struct Node
{
	int to;
	int w;
	int next;
}edge[maxe*3],edge1[maxe*3];

int cnt1,cnt2;

void init()
{
	cnt1 = 0;
	cnt2 = 0;
	memset(head,-1,sizeof(head));
	memset(ehead,-1,sizeof(ehead));	
}

void addEdge(int u,int v,int w)
{
	edge[cnt1].to = v;
	edge[cnt1].w = w;
	edge[cnt1].next = head[u];
	head[u] = cnt1++; 
}

void add_Edge(int u,int v,int w)
{
	edge1[cnt2].to = v;
	edge1[cnt2].w = w;
	edge1[cnt2].next = ehead[u];
	ehead[u] = cnt2++;
}

bool SPFA(int s,int n)
{
	int i,k;
	bool vis[maxe];
	int queue[maxe];
	int iq;
	int top;
	for(i=0;i<=n;i++)
	{
		dist[i] = inf;
	}
	memset(vis,false,sizeof(vis));
	memset(outque,0,sizeof(outque));
	iq = 0;
	queue[iq++] = s;
	vis[s] = true;
	dist[s] = 0;
	i = 0;
	while(i != iq)
	{
		top = queue[i];
		vis[top] = false;
		outque[top]++;
		if(outque[top] > n) return false;
		k = ehead[top];
		while(k >= 0)
		{
			if(dist[edge1[k].to] - edge1[k].w > dist[top])
			{
				dist[edge1[k].to] = dist[top] + edge1[k].w;
				if(!vis[edge1[k].to])
				{
					vis[edge1[k].to] = true;
					queue[iq] = edge1[k].to;
					iq++;
				}
			}
			k = edge1[k].next;
		}
		i++;
	}
	return true;
}

int a_star(int start,int end,int n,int k)
{
	Node1 e,ne;
	int cnt = 0;
	priority_queue que;
	if(start == end) k++; //      +1
	if(dist[start] == inf)
	{
		return -1;	
	}	
	e.to = start;
	e.g = 0;
	e.f = e.g + dist[e.to];
	que.push(e);
	while(!que.empty())
	{
		e = que.top();
		que.pop();
		if(e.to == end)
		{
			cnt++;
		}
		if(cnt == k)
		{
			return e.g;
		}
		for(int i=head[e.to];i != -1;i = edge[i].next)
		{
			ne.to = edge[i].to;
			ne.g = e.g + edge[i].w;
			ne.f = ne.g + dist[ne.to];
			que.push(ne);
		}
	}
	return -1;
}

int main()
{
	int m,n;
	int i;
	int a,b,time;
	int st,ed,ki;
	//freopen("1.txt","r",stdin);
	while(scanf("%d%d",&m,&n) != EOF)
	{
		init();
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d",&a,&b,&time);
			addEdge(a,b,time);
			add_Edge(b,a,time);	
		}
		scanf("%d%d%d",&st,&ed,&ki);
		SPFA(ed,m); //         
		printf("%d
",a_star(st,ed,m,ki)); } return 0; }

좋은 웹페이지 즐겨찾기