博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2449 Remmarguts' Date
阅读量:5318 次
发布时间:2019-06-14

本文共 4108 字,大约阅读时间需要 13 分钟。

Remmarguts' Date

Time Limit: 4000ms
Memory Limit: 65536KB
This problem will be judged on 
PKU. Original ID: 
64-bit integer IO format: %lld      Java class name: Main
 
"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 21 2 52 1 41 2 2

Sample Output

14

Source

 
解题:A*求第k短路。
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define LL long long14 #define pii pair
15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1010;18 struct arc{19 int to,w,next;20 arc(int x = 0,int y = 0,int z = -1){21 to = x;22 w = y;23 next = z;24 }25 };26 struct node{27 int g,h,to;28 node(int x = 0,int y = 0,int z = 0){29 g = x;30 h = y;31 to = z;32 }33 bool operator < (const node &x) const{34 return x.g + x.h < g+h;35 }36 };37 arc e[200010];38 int head[maxn],tail[maxn],d[maxn],tot,n,m,s,t,kth;39 int cnt[maxn];40 void add(int u,int v,int w){41 e[tot] = arc(v,w,head[u]);42 head[u] = tot++;43 e[tot] = arc(u,w,tail[v]);44 tail[v] = tot++;45 }46 priority_queue< pii,vector< pii >,greater< pii > >q;47 priority_queue
qq;48 void dijkstra(){49 for(int i = 1; i <= n; i++) d[i] = INF;50 while(!q.empty()) q.pop();51 d[t] = 0;52 q.push(make_pair(d[t],t));53 while(!q.empty()){54 int u = q.top().second;55 int w = q.top().first;56 q.pop();57 if(w > d[u]) continue;58 for(int i = tail[u]; ~i; i = e[i].next){59 if(d[e[i].to] > d[u] + e[i].w){60 d[e[i].to] = d[u] + e[i].w;61 q.push(make_pair(d[e[i].to],e[i].to));62 }63 }64 }65 }66 int Astar(){67 while(!qq.empty()) qq.empty();68 memset(cnt,0,sizeof(cnt));69 qq.push(node(0,d[s],s));70 while(!qq.empty()){71 node now = qq.top();72 qq.pop();73 if(++cnt[now.to] > kth) continue;74 if(cnt[t] == kth) return now.g;75 for(int i = head[now.to]; ~i; i = e[i].next)76 qq.push(node(now.g+e[i].w,d[e[i].to],e[i].to));77 78 }79 return -1;80 }81 int main() {82 int u,v,w;83 while(~scanf("%d %d",&n,&m)){84 memset(head,-1,sizeof(head));85 memset(tail,-1,sizeof(tail));86 for(int i = 0; i < m; i++){87 scanf("%d %d %d",&u,&v,&w);88 add(u,v,w);89 }90 scanf("%d %d %d",&s,&t,&kth);91 dijkstra();92 if(s == t) ++kth;93 printf("%d\n",Astar());94 }95 return 0;96 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3983907.html

你可能感兴趣的文章
【TOJ 2406】Power Strings(KMP找最多重复子串)
查看>>
hdu 1010
查看>>
keystone源码分析(一)——Paste Deploy的应用
查看>>
世界是座孤儿院
查看>>
VUE路由history模式坑记--NGINX
查看>>
线程同步-使用SimaphoreSlim类
查看>>
Spring整合MyBatis
查看>>
Java/Java Web中乱码解决汇总
查看>>
表格操作
查看>>
TortoiseGit使用指南
查看>>
大数据学习——securecrt同时向多个tab窗口发送相同的命令
查看>>
Swift学习笔记(4):字符串
查看>>
Windows下部署多个Tomcat
查看>>
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚
查看>>
VBoxManage命令速记
查看>>
(转载)我们工作到底为了什么 (HP大中华区总裁孙振耀退休感言)
查看>>
phpstudy + dvws
查看>>
2016/3/10 数据库简单操作( 创建数据库 创建表 数值类型 主键 外键 自动递增 )...
查看>>
Redis客户端连接池问题
查看>>
linux C print
查看>>