连接:http://poj.org/problem?id=2449
Remmarguts' Date
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17237 | Accepted: 4727 |
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.
"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).
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
//SPFA + A* 求K短路应该是比较高效的算法,由于SPFA的特殊性 //它的效率无法计算,对1000个点以内的图,下面的算发对于 //ACM竞赛还是可以的 #include<cstdio> #include<cstring> #include<queue> using namespace std; const int INF = 0x3fffffff; const int maxn = 1100;//点的规模 const int maxm = 110000;//边的规模 int dis[maxn],head[maxn],head1[maxn]; struct NODE{//求单源最短路用到 int to,w,next; }edge[maxm],edge1[maxm]; struct NODE1{//求K短路用到 int to; int g,f; bool operator < (const NODE1 &r)const { if(r.f==f)return r.g<g; return r.f<f; } }; bool SPFA(int s,int n,int head[maxn],NODE edge[maxn],int dist[maxn]) {//求s点到各个点的最短路,存到dist里面,如果有负圈,返回false int i,k; bool visit[maxn]; int queue[maxm];//注意别写错! int iq; int top; int outque[maxn]; for(i=0;i<=n;i++)dist[i]=INF; memset(visit,0,sizeof(visit)); memset(outque,0,sizeof(outque)); iq=0; queue[iq++]=s; visit[s]=true; dist[s]=0; i=0; while(i!=iq){ top=queue[i]; visit[top]=false; outque[top]++; if(outque[top]>n)return false; k=head[top]; while(k>=0){ if(dist[edge[k].to]-edge[k].w>dist[top]){ dist[edge[k].to]=dist[top]+edge[k].w; if(!visit[edge[k].to]){ visit[edge[k].to]=true; queue[iq]=edge[k].to; iq++; } } k=edge[k].next; } i++; } return true; } int a_star(int start,int end,int n,int k,int head[maxn],NODE edge[maxn],int dist[maxn]) {//A* 求出K短路,-1表示K短路不存在 NODE1 e,ne; int cnt=0; priority_queue<NODE1>que; if(start==end)k++;//如果s==t,0这条路不能算在K短路中,所以需要求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() { // freopen("in.txt","r",stdin); int n,m,a,b,w,s,t,k;//n个点,m条边 while(~scanf("%d%d",&n,&m)){ memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1));//记得要初始化 for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&w);//a点到b点权值为w edge[i].to=b; edge[i].w=w; edge[i].next=head[a]; head[a]=i;//存原图 edge1[i].to=a; edge1[i].w=w; edge1[i].next=head1[b]; head1[b]=i;//存反向图 } scanf("%d%d%d",&s,&t,&k);//起点s,终点t,K短路 SPFA(t,n,head1,edge1,dis); printf("%d\n",a_star(s,t,n,k,head,edge,dis)); } return 0; }
相关推荐
人们熟悉的四则运算表达式称为中缀表达式,例如(23+34*45/(5+6+7))。在程序设计语言中,可以利用堆栈的方法把中缀表达式转换成保值的后缀表达式(又称逆波兰表示法),并最终变为计算机可以直接执行的指令,得到...
【标题】"POJ1691-Painting A Board 【拓扑+DFS】"是一个源自北京大学在线编程平台POJ的编程题目,它主要涉及到图论中的拓扑排序和深度优先搜索(DFS)算法。该题目的核心是解决一个与涂色相关的实际问题,通过运用...
【标题】"POJ1426-Find The Multiple【BFS+同余模】"是一道来源于北京大学在线编程平台POJ的算法题目,主要涉及到了广度优先搜索(BFS)与同余模运算的知识点。这道题目要求解决的是寻找一个整数的倍数问题,可能...
### POJ2449 Remmarguts'Date **题目链接:** <http://acm.pku.edu.cn/JudgeOnline/problem?id=2449> **核心知识点:** - **算法选择:** Dijkstra + A* (A-Star)。 - **问题背景:** 给定一张带权图,求出两个...
标题中的"POJ1002-487-3279【Hash+Qsort】"是指一个编程挑战题目,通常在在线编程平台上出现,比如北京大学的Peking Online Judge (POJ)。这个题目结合了哈希表(Hash)和快速排序(Qsort)两种算法来解决问题。哈希...
【标题】"POJ中级搜索全部练习【解题报告+AC代码】"涉及的是编程竞赛中的一个重要主题——搜索算法在解决POJ(Problemset of Peking University)中级问题的应用。POJ是北京大学主办的一个在线判题系统,为编程爱好...
【Euler Path】,欧拉路径是指在一个有向或无向图中,从一个顶点出发,经过图中每条边恰好一次后回到起点的路径。在本题中,欧拉路径可能用于构建一种从颜色序列中提取信息的模型,例如通过遍历特定的欧拉路径来分析...
2505 2521 2538 2546 2551 2590 2593 2601 2665 2680 2739 2752 2761 2762 2777 2800 2891 2893 2992 3030 3041 3132 3159 3187 3204 3270 3277 3281 3297 3321 3414 3436 3461 3650 3663 3664 3672 3740
POJ2449 - Remmarguts’ Date - **题目链接**:[POJ2449](http://acm.pku.edu.cn/JudgeOnline/problem?id=2449) - **解法概述**:此题可通过 Dijkstra 算法结合 A* 搜索策略来求解。主要考察的是如何在有向图中...
+ poj1860, poj3259, poj1062, poj2253, poj1125, poj2240 * 最小生成树算法:prim, kruskal + poj1789, poj2485, poj1258, poj3026 * 拓扑排序:poj1094 * 二分图的最大匹配:匈牙利算法 + poj3041, poj3020 * ...
接下来,POJ2449是中等难度的题目,要求在有向图中找到从源点S到汇点T的第K短路径的长度。这里使用了A*算法,这是一种启发式搜索算法,结合了Dijkstra算法和最佳优先搜索。A*算法通过评估函数f(x)=g(x)+h(x)来指导...
《POJ3009-Curling 2.0:深度优先搜索、向量、回溯与剪枝的综合应用》 北京大学在线编程平台上的POJ3009题目名为"Curling 2.0",它是一道涉及到算法与数据结构的复杂问题,主要考察了参赛者对深度优先搜索(DFS)、...
【标题】"POJ2586-Y2K Accounting Bug" 是一道来自北京大学在线判题系统POJ(Problem Set)的编程题目。该题目主要关注的是计算机编程中的一个历史问题——Y2K千年虫问题(Year 2000 Problem),同时也涉及到会计...
- **例题**:poj1860, poj3259, poj1062, poj2253, poj1125, poj2240 - **解释**:最短路径算法包括Dijkstra算法、Bellman-Ford算法、Floyd算法以及堆优化的Dijkstra算法等。 ##### (3) 最小生成树算法 - **例题**...
### POJ 分类题目知识点详解 #### 一、基本算法 **1. 枚举** - **定义**:枚举是一种通过尝试所有可能情况来解决问题的方法。 - **示例题目**: - poj1753 - poj2965 - **应用场景**:适用于问题规模较小或解决...
适用于带权重的有向图和无向图。 #### 1087 *A Plug for UNIX - 二分匹配 - **知识点**:二分匹配是解决一类特定问题的有效算法,特别是当问题可以被建模为一个二分图时尤为有效。 - **二分图**:一种特殊的图,...
【标题】"POJ3733-Changing Digits【DFS+强剪枝】"是一个来自北京大学在线评测系统POJ的编程题目,该题目主要涉及深度优先搜索(DFS)算法和强剪枝策略。在算法竞赛和编程挑战中,这类问题通常要求参赛者通过编写...
《POJ3026-Borg Maze:BFS与Prim算法的应用解析》 在计算机科学领域,图论是解决问题的重要工具之一,而BFS(广度优先搜索)和Prim算法则是其中的两种经典算法。本篇文章将围绕北大POJ3026题目“Borg Maze”来探讨...
- **拓扑排序**:对有向无环图的排序,如poj1094。 - **二分图最大匹配**:匈牙利算法,如poj3041和poj3020,解决分配问题。 - **最大流的增广路算法**:KM算法,如poj1459和poj3436,用于寻找网络中能传输的最大...