计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]
题目链接:https://nanti.jisuanke.com/t/31001

题意:
一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某一条边的权变成 0,问他走到节点 n 的最小权值为多少。
题解:
将dist数组和vis数组都扩展一维:
dist[c][i]代表:已经使用了 c 次变0魔法后,走到节点 i 的最短距离;
vis[c][i]代表:已经使用了 c 次变0魔法后,走到节点 i 的最短距离,这个最短距离是否已经被准确计算完毕。
然后就是稍微改动一下原来的优先队列优化Dijkstra即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const int maxn=1e5+;
const int maxm=2e5+;
const int maxk=;
const ll INF=0x3f3f3f3f3f3f3f3f; int n,m,k; struct Edge{
int u,v;
ll w;
Edge(int u=,int v=,ll w=){this->u=u,this->v=v,this->w=w;}
};
vector<Edge> E;
vector<int> G[maxn];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void addedge(int u,int v,ll w)
{
E.push_back(Edge(u,v,w));
G[u].push_back(E.size()-);
} struct Qnode{
int vertex;
ll dist;
int cnt;
Qnode(int v=,ll d=,int c=){this->vertex=v,this->dist=d,this->cnt=c;}
bool operator <(const Qnode &oth)const{
return dist>oth.dist;
}
//优先队列默认是降序排列,如果本节点小于另一个节点的意义是 本节点dist < 另一个节点dist,
//则优先队列的队首放的就是最大的节点,就是dist最大的节点,显然我们要的是相反的情况,
//所以设置本节点小于另一个节点的意义是 本节点dist > 另一个节点dist 即可。
}; ll dist[maxk][maxn];
bool vis[maxk][maxn];
void dijkstra(int s)
{
for(int i=;i<=n;i++)
{
for(int c=;c<=k;c++)
{
dist[c][i]=((i==s)?:INF);
vis[c][i]=;
}
} priority_queue<Qnode> Q;
Q.push(Qnode(s,,));
while(!Q.empty())
{
int u=Q.top().vertex, c=Q.top().cnt; Q.pop();
if(vis[c][u]) continue;
vis[c][u]=;
for(int i=;i<G[u].size();i++)
{
Edge &e=E[G[u][i]]; int v=e.v;
if(!vis[c][v] && dist[c][v]>dist[c][u]+e.w)
{
dist[c][v]=dist[c][u]+e.w;
Q.push(Qnode(v,dist[c][v],c));
}
if(c+<=k && !vis[c+][v] && dist[c+][v]>dist[c][u])
{
dist[c+][v]=dist[c][u];
Q.push(Qnode(v,dist[c+][v],c+));
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
init(,n);
for(int i=;i<=m;i++)
{
int u,v; ll w;
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w);
}
dijkstra();
ll ans=INF;
for(int c=;c<=k;c++) ans=min(ans,dist[c][n]);
printf("%lld\n",ans);
}
}
计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]的更多相关文章
- 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...
- 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]
题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...
- 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...
- 计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/30990 Alice, a student of grade 6, is thinking about an Olympian M ...
- 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...
- 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]
题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...
- 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...
- 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...
随机推荐
- [OpenCV] Samples 01: Geometry - 几何图形
前言 基本的几何图形,标注功能. commondLineParser的使用参见:http://blog.csdn.net/u010305560/article/details/8941365 #inc ...
- [Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程
Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be cre ...
- 让树莓派自动上报IP地址到邮箱,二代B
由于我使用树莓派的场景大多数是在没有显示器.只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它.然后我又要很麻烦地登录路由 ...
- YUV格式学习笔记
YUV与RGB表现图像的方法不同,其采用的是一个亮度信号加两个色差信号的方式来表现图像.其中UV表示的是CbCr,常见的YUV格式有:YUV4:2:0,YUV4:2:2,YUV4:1:1,YUV4:4 ...
- pip导出安装包及批量安装
python导出安装包及版本 pip freeze > requirements.txt 批量安装pip install -r requirements.txt
- js当中null和{}区别
{}是一个不完全空的对象,因为他的原型链上还有Object呢,而null就是完全空的对象,啥也没有,原型链也没有,所以null instanceof Object === false;[]就更不用说了 ...
- 在javaweb的项目当中实现随机数字的生成
首先,需要在javaweb的项目当中新建一个Servlet文件,然后再web.xml中配置一下: 这样运行的时候就可以通过“http://localhost:8080/Response/Respons ...
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
本文转载至 http://blog.csdn.net/u014011807/article/details/40187737 在本卷你可以学到什么? 采用四种方法设计应用于各种场合的音频播放器: 基于 ...
- Mybatis的resultMap返回map
<resultMap type="Map" id="bankMaintainMap"> <result column="bank_n ...
- Oracle和Mysql中mybatis模糊匹配查询区别
1.Oracle AND NAME LIKE '%'||#{name}||'%' 2.Mysql AND NAME LIKE "%"#{name}"%"