连接: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; }
相关推荐
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
北大POJ2513-Colored Sticks【TrieTree+MergeSet+EulerPath】 解题报告+AC代码
北大POJ1002-487-3279【Hash+Qsort】 解题报告+AC代码
人们熟悉的四则运算表达式称为中缀表达式,例如(23+34*45/(5+6+7))。在程序设计语言中,可以利用堆栈的方法把中缀表达式转换成保值的后缀表达式(又称逆波兰表示法),并最终变为计算机可以直接执行的指令,得到...
北大POJ中级搜索全部练习【解题报告+AC代码】
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
北大POJ2586-Y2K Accounting Bug 解题报告+AC代码
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
int Gcd(int a, int b) { while(b != 0) { int r = b; b = a % b; a = r; } return a; } _int64 exGcd(__int64 a, __int64 b) { if(b==0) { x=1; y=0; return a; } __int64 r=exGcd(b, a%b); __int...
北大POJ3026-Borg Maze【BFS+Prim】 解题报告+AC代码
北大POJ初级-所有题目AC代码+解题报告
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
POJ2186-Popular Cows 【Tarjan+极大强连通分量+缩点】 解题报告+AC代码 http://hi.csdn.net/!s/BGDH68 附:我所有的POJ解题报告链接 . http://blog.csdn.net/lyy289065406/article/details/6642573
北大POJ1936-All in All 解题报告+AC代码
北大POJ3349-Snowflake Snow Snowflakes 解题报告+AC代码
北大POJ1027-The Same Game 解题报告+AC代码
East Central North America 1999。50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50...
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类