题目链接: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. 泛泰A870K去掉相机快门声音的方法

    首先ROOT手机,挂载读写,/system/media/audio/ui里面哈,把camera-click.ogg改成camera-click.ogg.bak就可以了 转载自:http://bbs.9 ...

  2. Android图片的合成示例

    package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.grap ...

  3. 【数据分析】Superset 之二 Docker安装初始化

    docker search superset amancevice/superset [] Superset on Debian/Python3 [OK] docker pull amancevice ...

  4. 【安全开发】C/C++安全编码规范

    C本质上是不安全的编程语言.例如如果不谨慎使用的话,其大多数标准的字符串库函数有可能被用来进行缓冲区攻击或者格式字符串攻击.但是,由于其灵活性.快速和相对容易掌握,它是一个广泛使用的编程语言.下面是针 ...

  5. 【RF库XML测试】Get Element Text

    Name:Get Element TextSource:XML <test library>Arguments:[ source | xpath=. | normalize_whitesp ...

  6. FPGA连接

    别人推荐,暂时存这了: http://www.eepw.com.cn/news/fpga

  7. 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  8. C++中class与struct的区别(struct的类型名同时可以作为变量名)

    通常我们知道的区别: (一)默认继承权限.如果不明确指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理: (二)成员的默认访问权限.class的成员默 ...

  9. bigdecimal 与long int 之间转换

    BigDecimal与Long.int之间的互换 在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigD ...

  10. hive报错: Specified key was too long; max key length is 767 bytes

    废话不多说,报错如下: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while addi ...