Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30772   Accepted: 8397

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短路
A*
估价函数为dis(起点,i)+dis(i,终点) 1、反向图上求出终点到每个点的最短路
2、起点入优先队列,
  队首出队,
  如果队首是终点,而且是第k次出队,
那么当前距离就是k短路
如果队首不是终点,便利与当前点连接的所有的点,入队 细节1:优先队列出入队不用vis数组判重,因为边可以重复走
细节2:如果第1步中,起点与终点不连通,输出-1结束,
    否则进入A*,没有vis数组,出现环会死循环
细节3:如果起点=终点,令k++,因为起点会立即出队
#include<queue>
#include<cstdio>
#include<cstring>
#define N 1001
#define M 100001
using namespace std;
int n,s,t,k;
int dis1[N];
bool vis[N];
int front[N],to[M],nxt[M],val[M],tot;
int front2[N],to2[M],nxt2[M],val2[M],tot2;
struct node
{
int num,dis;
bool operator < (node p) const
{
return dis+dis1[num]>p.dis+dis1[p.num];
}
}now,nt;
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w;
to2[++tot2]=u; nxt2[tot2]=front2[v]; front2[v]=tot2; val2[tot2]=w;
}
void init()
{
int m,u,v,w;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
}
void spfa()
{
memset(dis1,,sizeof(dis1));
queue<int>q;
dis1[t]=;
vis[t]=true;
q.push(t);
int now;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=false;
for(int i=front2[now];i;i=nxt2[i])
if(dis1[to2[i]]>dis1[now]+val2[i])
{
dis1[to2[i]]=dis1[now]+val2[i];
if(!vis[to2[i]])
{
q.push(to2[i]);
vis[to2[i]]=true;
}
}
}
}
void Astar()
{
if(dis1[s]>1e9)
{
printf("-1");
return;
}
if(s==t) k++;
int cnt=,last=-;
priority_queue<node>q;
now.num=s;
now.dis=;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(now.num==t)
{
cnt++;
if(cnt==k)
{
printf("%d",now.dis);
return;
}
}
for(int i=front[now.num];i;i=nxt[i])
{
nt.num=to[i];
nt.dis=now.dis+val[i];
q.push(nt);
}
}
printf("-1");
}
int main()
{
init();
spfa();
Astar();
}
												

最新文章

  1. 设计模式--建造者模式Builder(创建型)
  2. PHP+Nginx环境搭配
  3. CSS3弹性伸缩布局(一)——box布局
  4. 【转】浅析linux内存模型
  5. C#将HTML导出Excel
  6. Python学习笔记 第二课 循环
  7. javascript设计模式5
  8. C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
  9. 关于mtk Android打开串口权限问题
  10. iOS 圆角那些事(转)
  11. 【翻译】使用Visual Studio在Azure上部署Asp.Net Core Web应用
  12. open_links_per_instance 和 open_links 参数说明
  13. spring cloud 创建一个简单Eureka Server
  14. Nodejs通过账号密码连接MongoDB数据库
  15. L0 Regularization
  16. Javascript异步编程的4种方法(阮一峰)
  17. 尚硅谷redis学习4-数据类型
  18. include方便查找
  19. 团体程序设计天梯赛L3-019 代码排版(23分)
  20. Mac OS Yosemite 文件批量重命名

热门文章

  1. 【转】Expressions versus statements in JavaScript
  2. Fafa and the Gates(模拟)
  3. 第二十次ScrumMeeting会议
  4. 软件工程课堂作业(三)——Right-BICEP软件单元测试
  5. gcc 学习笔记(一) - 编译C程序 及 编译过程
  6. unity 学习记录
  7. 总结get和post区别
  8. Activemq 消息类型 (转)
  9. C#常见函数
  10. [剑指Offer] 59.按之字形顺序打印二叉树