Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30017   Accepted: 8159

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

A* + 最短路
http://blog.csdn.net/b2b160/article/details/4057781 贴个网站 A*算法详解
估价函数 = 当前值+当前位置到终点的距离
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N= ;
const int M= ;
const int INF=0x3f3f3f3f;
int tot,head[N],re[N],d[N],st,ed,k;
bool vis[N];
struct nnn{
   int to,next,w;
}e[M],e1[M];
void add(int u,int v,int w){
   e[tot].to=v;
   e[tot].next=head[u];
   e[tot].w=w;
   head[u]=tot;
   e1[tot].to=u;
   e1[tot].next=re[v];
   e1[tot].w=w;
   re[v]=tot++;
}
struct node{
    int f,u,g;
    bool operator <(const node &A)const{
      return A.f<f;
    }
};
void pre(){
    d[ed]=;
    memset(vis,,sizeof(vis));
    memset(d,INF,sizeof(d));
    queue<int>Q;
    Q.push(ed);
    d[ed]=;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        vis[u]=;
        for(int i=re[u];i+;i=e1[i].next){
            int v=e1[i].to;
            if(d[v]>d[u]+e1[i].w){
                d[v]=d[u]+e1[i].w;
                if(!vis[v]) {
                    Q.push(v);
                    vis[v]=;
                }
            }
        }
    }
}
int slove(){
    int cont=;
    memset(vis,,sizeof(vis));
    priority_queue<node>Q;
    node p,q;
    p.f=d[st],p.u=st,p.g=;
    Q.push(p);
    if(p.f==INF) return -;
    while(!Q.empty()){
        p=Q.top();
        Q.pop();
        if(p.u==ed){
            ++cont;
            if(cont==k) return p.g;
        }
        for(int i=head[p.u];i+;i=e[i].next){
            int v=e[i].to;
            q.u=v;q.g=p.g+e[i].w;q.f=q.g+d[v];
            Q.push(q);
        }
    }
    return -;
}
int main(){
    int n,m,u,v,w;
    while(scanf("%d%d",&n,&m)!=EOF){
        tot=;
        memset(head,-,sizeof(head));
        memset(re,-,sizeof(re));
        while(m--){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        scanf("%d%d%d",&st,&ed,&k);
        pre();
        if(st==ed) ++k;
        printf("%d\n",slove());
    }
}

poj2449第K小路径问题的更多相关文章

  1. SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)

    题意: 求树上A,B两点路径上第K小的数 分析: 同样是可持久化线段树,只是这一次我们用它来维护树上的信息. 我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表 ...

  2. Count on a tree(树上路径第K小)

    题目链接:https://www.spoj.com/problems/COT/en/ 题意:求树上A,B两点路径上第K小的数 思路:主席树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上. ...

  3. SPOJ 10628 求树上的某条路径上第k小的点

    第k小,很容易会想到用主席树来解决 这里简单想一下树的转移过程 因为本身无向图形成一棵树,那么我们总以1为根,那么之后连下去的边对应的点建立的线段树总是在父亲节点对应的树上加上一个当前点对应位置出现的 ...

  4. 树上第k小,可持久化线段树+倍增lca

    给定一颗树,树的每个结点都有权值, 有q个询问,每个询问是 u v k ,表示u到v路径上第k小的权值是多少. 每个结点所表示的线段树,是父亲结点的线段树添加该结点的权值之后形成的新的线段树 c[ro ...

  5. 主席树总结(经典区间第k小问题)(主席树,线段树)

    接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...

  6. [BZ4923][Lydsy1706月赛]K小值查询

    K小值查询 题面 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. Input ...

  7. SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解

    题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...

  8. POJ 2449 Remmarguts' Date (第k短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

  9. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

随机推荐

  1. Spring boot自定义parent POM

    文章目录 概述 不使用Parent POM来引入Spring boot 覆盖依赖项版本 概述 在之前的Spring Boot例子中,我们都会用到这样的parent POM. <parent> ...

  2. div/dom元素拖拽缩放插件,纯js实现拖拽缩放,不依赖jQuery~

    产品需求,需要用到对div(dom)进行拖拽缩放操作,看到有好多插件,要么依赖jQuery,要么文件太大. 封装了一个插件,不压缩状态下5KB. html <!DOCTYPE html> ...

  3. 狄慧201771010104《面向对象程序设计(java)》第十六周学习总结

    实验十六  线程技术 实验时间 2017-12-8 一.知识点总结: 1.程序与进程的概念 ‐程序是一段静态的代码,它是应用程序执行的蓝本. ‐进程是程序的一次动态执行,它对应了从代码加载.执行至执行 ...

  4. Spring MVC之LocaleResolver详解

    2019独角兽企业重金招聘Python工程师标准>>> 对于LocaleResolver,其主要作用在于根据不同的用户区域展示不同的视图,而用户的区域也称为Locale,该信息是可以 ...

  5. JS省城级联

    2019独角兽企业重金招聘Python工程师标准>>> 这里是HTML内容 <label class="control-label col-md-2 col-sm-3 ...

  6. Xapian实战(四):搜索

    参考资料: 学习Xapian(1)-基础的建索引和搜索 1. Xapian中用于搜索的类 Enquire - 提供了检索的接口:(Enquire API) QueryParser(QueryParse ...

  7. MYSQL 排序和分组

    一.MYSQL 中有两种排序方式: 1:通过有序索引顺序扫描直接返回有序数据,这种方式在使用explain 分析查询的时候显示为Using Index ,不需要额外的排序,操作效率较高. 2: 是通过 ...

  8. USACO Training Section 1.1 坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  9. FarmCraft

    题意:mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子.mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为ci.树上的每 ...

  10. C++中const的特性

    目录(作用): 1:修饰变量,说明该变量不可以被改变: 2:修饰指针,分为只想常量的指针和自身是常量的指针 3:修饰引用,指向常量的引用,用于修饰形参,即避免了拷贝,有避免了函数对值的修改: 4:修改 ...