题目链接: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. C语言realloc,malloc,calloc的区别【转载】

    转载自:http://www.cnblogs.com/BlueTzar/articles/1136549.html 三个函数的申明分别是: void* realloc(void* ptr, unsig ...

  2. SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定

    springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...

  3. Hibernate的Configuration和SessionFactiory

    Configuration: Configuration是hibernate的入口,负责管理Hibernate的配置信息,这些配置信息都是从配置文件hibernate.cfg.xml或者Hiberna ...

  4. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  5. sql 替换字符串

    MSSQL替换语句: )),'abc.com','123.com') 例如: update PE_Article set Content=replace(cast(Content as varchar ...

  6. C语言中一个字符数组里面的所有元素变成一个字符串

    #include <string.h> int main() // 这里为了方便直接用main函数 {     char array[] = { 'h', 'e', 'l', 'l', ' ...

  7. css媒体查询来书写二倍图三倍图设置

    @media (-webkit-min-device-pixel-ratio: 2){} @media (-webkit-min-device-pixel-ratio: 3){}

  8. Android开发训练之第五章——Building Apps with Connectivity & the Cloud

    Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...

  9. 子页面iframe跨域执行父页面定义的JS方法

    问题需求:父页面与子页面iframe跨域嵌套,子页面要触发父页面所定义的js方法.父子页面的数据传递. 下文中会用到一些文件:父页面: parent.html嵌在父页面的子iframe页面:child ...

  10. 百度编辑器(UEditor)自定义工具栏

    百度编辑器(UEditor)自定义工具栏的自定义 百度编辑器默认功能比较齐全,但是不一定是我们所需要的,有的功能可以去掉,用自己想要的就可以了,可以参考百度官方文档! 百度编辑器默认配置展示界面 如何 ...