示例:

输入:

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;
}

最新文章

  1. 【.net 深呼吸】记录WCF的通信消息
  2. 实践.Net Core在Linux环境下的第一个Hello World
  3. elastichq 离线安装
  4. PHP异常处理函数set_exception_handler()的用法
  5. c语言第8次作业
  6. Apache设置页面认证(原创贴-转载请注明出处)
  7. JavaScript 随机链接
  8. iOS tableView 静态单元格的实现
  9. Javascript的变量与delete操作符
  10. kaili 2.0 metasploit连接postgres数据库
  11. EIGRP汇总
  12. Spring3 M2 quartz-2.1.7 解决bean不能注入问题
  13. 让rpc支持双向通信
  14. 根据IP查地理位置信息
  15. spring boot中的jave注解学习
  16. Zookeeper 配置集群环境详解
  17. WPF背景透明内嵌WebBrowser不显示问题,即AllowsTransparency = true 和 Webbrowser 等控件显示冲突
  18. google3aac509c9040e79d
  19. Swift网络封装库Moya中文手册之Endpoints
  20. UTF-8编码占几个字节?

热门文章

  1. python中序列的操作
  2. 关于System.AccessViolationException异常
  3. redis 设置为只读模式
  4. 2-开发共享版APP(接入指南)-设备接入说明:快速接入
  5. vue 把后端返回的图片和url链接生成的二维码用canvas 合成一张图片
  6. nodejs之express生成项目[windows平台]
  7. JAVA学习网站分享
  8. 第07组 Alpha冲刺(6/6)
  9. 剑指offer:数组中只出现一次的数字
  10. Activiti6 应用安装 activiti-admin,activiti-app,activiti-rest