示例:

输入:

3 2 1 3 1
1 2 1
2 3 2

输出:1

题意:求s,t最短路,可将k条边权值置零。

题解:分层图最短路原题

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn = 1e5+;
const int INF = 0x3f3f3f3f;
struct State
{
// 优先队列的结点结构体
int v, w, cnt; // cnt 表示已经使用多少次免费通行权限
State() {}
State(int v, int w, int cnt) : v(v), w(w), cnt(cnt) {}
bool operator<(const State &rhs) const
{
return w > rhs.w;
}
};
struct node
{
int v;
int w;
int next;
/* data */
} edge[maxn];
priority_queue<State>pq;
int n,t,m,k,u,v,w,s;
int cnt;
bool vis[maxn][];
int dis[maxn][];
int head[maxn]; void add(int u,int v,int w) //链式前向星存边
{
edge[cnt] = {v,w,head[u]};
head[u] = cnt++;
}
void dijkstra()
{
memset(dis, 0x3f, sizeof(dis));
dis[s][] = ;
pq.push(State(s, , )); // 到起点不需要使用免费通行权,距离为零
while (!pq.empty())
{
State top = pq.top();
pq.pop();
int u = top.v;
int nowCnt = top.cnt;
if (vis[u][nowCnt])
continue;
vis[u][nowCnt] = true; for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].v, w = edge[i].w;
if (nowCnt < k && dis[v][nowCnt + ] > dis[u][nowCnt])
{
// 可以免费通行
dis[v][nowCnt + ] = dis[u][nowCnt];
pq.push(State(v, dis[v][nowCnt + ], nowCnt + ));
}
if (dis[v][nowCnt] > dis[u][nowCnt] + w)
{
// 不可以免费通行
dis[v][nowCnt] = dis[u][nowCnt] + w;
pq.push(State(v, dis[v][nowCnt], nowCnt));
}
}
}
} int main()
{
memset(head,-,sizeof (head));
scanf("%d%d%d%d%d",&n,&m,&s,&t,&k);
while (m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u, v, w);
add(v, u, w);
}
int ans = INF;
dijkstra();
for (int i = ; i <= k; ++i)
ans = min(ans, dis[t][i]); // 对到达终点的所有情况取最优值
cout << ans << endl;
}

free(分层图最短路)(2019牛客暑期多校训练营(第四场))的更多相关文章

  1. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  2. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  3. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  7. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 2019牛客暑期多校训练营(第二场)J-Subarray(思维)

    >传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...

  9. 2019牛客暑期多校训练营(第二场)D bitset

    题意 给一个n个结点的带点权的图,找到第k小的团的权值 分析 用bitset表示团的状态,一个结点必须和团里的每个结点都连边才能加进去,所以可以直接用\(\&\)运算来判断一个结点是否能加进去 ...

  10. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

随机推荐

  1. 从一道题浅说 JavaScript 的事件循环

    最近看到这样一道有关事件循环的前端面试题: //请写出输出内容 async function async1() { console.log('async1 start'); await async2( ...

  2. BZOJ 1477: 青蛙的约会

    二次联通门 : BZOJ 1477: 青蛙的约会 /* BZOJ 1477: 青蛙的约会 扩展欧几里得 列出方程, 判断一下 */ #include <iostream> #include ...

  3. 第03组 Alpha冲刺(1/4)

    队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...

  4. 64、Spark Streaming:StreamingContext初始化与Receiver启动原理剖析与源码分析

    一.StreamingContext源码分析 ###入口 org.apache.spark.streaming/StreamingContext.scala /** * 在创建和完成StreamCon ...

  5. fillter根据value来匹配字段

    字段对应 let cashBackState = { 'WAIT_FIVE': '满5单可返现', 'FINISHED': '已返现' } filters: { cashBackStateFilter ...

  6. Join Reorder优化 - 论文摘要

    Query Simplification: Graceful Degradation for Join-Order Optimization 这篇的related work可以参考,列的比较全面, Q ...

  7. [linux]杀死同一个应用的所有进程

    ps -ef|grep "c.py"|grep -v grep|awk '{print $2}' ps -ef|grep "c.py"|grep -v grep ...

  8. PostgreSQL中的partition-wise aggregation

    partition-wise aggregation允许对每个分区分别执行的分区表进行分组或聚合.如果GROUP BY子句不包括分区键,则只能在每个分区的基础上执行部分聚合,并且必须稍后执行最终处理. ...

  9. 善用mysql中的FROM_UNIXTIME()函数和UNIX_TIMESTAMP()函数

    我们经常会面临要从数据库里判断时间,取出特定日期的查询.但是数据库里储存的都是unix时间戳,处理起来并不是特别友好.幸而MYSQL提供了几个处理时间戳的函数,可以帮助我们在查询的时候,就将时间戳格式 ...

  10. python实现读取并显示图片的两种方法

    https://www.cnblogs.com/lantingg/p/9259840.html 在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片. ...