题目链接: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题]的更多相关文章

  1. 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...

  2. 计蒜客 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 样例输入: ...

  3. 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...

  4. 计蒜客 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 ...

  5. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...

  6. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  7. 计蒜客 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 ...

  8. 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...

  9. 计蒜客 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 ...

随机推荐

  1. js数组获取相同元素个数,归档排序

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. ios开发之--仿购物类详情页面数量添加小功能

    话不多说先上图:

  3. 转载nginx+uwsgi+django

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  4. 第一个map reduce程序

    完成了第一个mapReduce例子,记录一下. 实验环境: hadoop在三台ubuntu机器上部署 开发在window7上进行 hadoop版本2.2.0 下载了hadoop-eclipse-plu ...

  5. 利用DB实现简单去重处理

    业务需要针对某文件进行判重操作,用Perl实现如下 #!/usr/bin/perl my %h; s/\s+$// and $h{$_}++ while <>; print "$ ...

  6. 据库分库分表(sharding)系列(一) 拆分实施策略和示例演示

    本文原文连接: http://blog.csdn.net/bluishglc/article/details/7696085 ,转载请注明出处!本文着重介绍sharding切分策略,如果你对数据库sh ...

  7. java 对 汉字排序(按照拼音字母排序)

    业务场景: 一个list集合,里面add了若干个实体类,针对该实体类排序的属性为String. 使用技术,自定义list排序(JDK自带),重写Comparator接口的compare方法,汉字转拼音 ...

  8. 多了解一下Chrome开发者控制台

    多了解一下Chrome开发者控制台 2017年10月14日 • Tools, Web前端 • 1.0k views • 暂无评论 作为一名前端开发者,Chrome内置的控制台是必须了解的,它拥有非常丰 ...

  9. Qt获取CPU编号和硬盘序列号

    windows下执行命令除了用cmd之外,还有个东西叫WMIC,非常强大,可以通过他获取很多信息,包括硬件信息. QString frmMain::getWMIC(const QString & ...

  10. 【Spring源码分析系列】加载Bean

    /** * Create a new XmlBeanFactory with the given input stream, * which must be parsable using DOM. * ...