ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)
题目链接:https://nanti.jisuanke.com/t/31001
题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0。
样例输入 复制
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
样例输出 复制
3 解题思路:可以用两种做法,不过都差不多,应该算是同一种思路的不同写法。
第一种是在建图时,将一个点拆成k个层次的点,应该总共有k+1层,每个相同层次的点按输入的边权连接,每个点可以向它能连接到的点的下一个层次连接一条边权为0的边,这样你每使一条边权值变为0,即相当于走到了下一层图,永远不能走回,当走到第k+1层图时即不能使用了,在这个含有k+1层图的大图里跑下最短路就可以得出答案了
附上代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=;
const ll inf=0x3f3f3f3f;
struct qnode{
int u;
ll dis;
bool operator<(const qnode &a)const{
return dis>a.dis;
}
qnode(int a,ll b)
{
u=a;
dis=b;
}
};
struct node{
int v,w,next;
}edge[*maxn];
int n,m,k,tot=,head[maxn];
ll dis[maxn];
void add(int u,int v,int w)
{
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
void dij()
{
priority_queue<qnode> que;
memset(dis,inf,sizeof(dis));
dis[]=;
que.push(qnode(,));
while(!que.empty())
{
int u=que.top().u;
que.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
que.push(qnode(v,dis[v]));
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
for(int j=;j<=k;j++)
{
add(u+j*n,v+j*n,w); //同一层次的点用输入边权相连
if(j!=k)
add(u+j*n,v+(j+)*n,); //不同层次的点用0权值相连
}
}
if(k>=m)
{
printf("0\n");
continue;
}
ll ans=inf;
dij();
for(int i=;i<=k;i++)
ans=min(ans,dis[n+i*n]);
printf("%lld\n",ans);
}
return ;
}
第二种是用最短路+dp思想,再开一维数组记录已经使用了几次使边的权值为0,也是跑下最短路就可以了。
附上代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
const long long inf=0x3f3f3f3f;
struct node{
int v,w,next;
}edge[*maxn];
struct qnode{
int u,k;
long long dis;
bool operator<(const qnode &a)const{
return dis>a.dis;
}
qnode(int a,int b,long long c)
{
u=a;
k=b;
dis=c;
}
};
int n,m,k,tot=,head[maxn];
long long dis[maxn][];
void add(int u,int v,int w)
{
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
long long dijkstra()
{
priority_queue<qnode> que;
for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
{
dis[i][j]=inf;
}
}
que.push(qnode(,,));
dis[][]=;
while(!que.empty())
{
int u=que.top().u,tempk=que.top().k;
if(u==n)
return dis[u][tempk];
que.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(dis[u][tempk]+w<dis[v][tempk]) //同一层的最短路
{
dis[v][tempk]=dis[u][tempk]+w;
que.push(qnode(v,tempk,dis[v][tempk]));
}
if(tempk<k)
{
if(dis[u][tempk]<dis[v][tempk+]) //如果将这条边权值变为0,就会进入tempk+1层
{
dis[v][tempk+]=dis[u][tempk];
que.push(qnode(v,tempk+,dis[v][tempk+]));
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%lld\n",dijkstra());
}
return ;
}
ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)的更多相关文章
- ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】
<题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)
题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...
- ACM-ICPC 2018 南京赛区网络预赛 E题
ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...
- ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze
262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v ...
- ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)
There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...
- ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图
类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...
- ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)
题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...
- ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路
https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析 分层最短路 我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...
随机推荐
- Vue+iview实现添加删除类
<style> .tab-warp{ border-bottom: solid 1px #e0e0e0; overflow: hidden; margin-top: 30px; posit ...
- java线程池实现原理
(1):线程池存在哪些状态,这些状态之间是如何进行切换的呢? (2):线程池的种类有哪些? (3):创建线程池需要哪些参数,这些参数的具体含义是什么? (4):将任务添加到线程池之后运行流程? (5) ...
- mac下virtualbox中centos6.5虚拟机实现全屏和调整分辨率
在visualbox里安装好centos后,发现不能分辨率与原屏幕不一致,很多解决方法是:安装增强包.可是安装增强包后依然达不到效果. 究其原因,原来因为没有安装显卡驱动导致安装了增强包后无法实现分辨 ...
- If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
学习Spring Boot 过程中遇到了下列这个问题 Description: Failed to configure a DataSource: 'url' attribute is not spe ...
- Yii2后台管理系统常规单据模块最佳实践
后台管理系统的常规单据通常包括数据,页面,功能:其中数据,页面,功能又可以细分如下: 分类 二级分类 主要内容 注意事项 例如 数据 数据库迁移脚本 用于数据表生成及转态回滚 1.是否需要增 ...
- [oracle] to_date() 与 to_char() 日期和字符串转换
to_date("要转换的字符串","转换的格式") 两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. to_char(日期,& ...
- How to enable usb on vbox
Device-->Install Guest Addition Shared Folders Settings-->Advanced-->Shared Clipboard--> ...
- 【数学建模】day10-主成分分析
0. 关于主成分分析的详细理解以及理论推导,这篇blog中讲的很清楚. 主成分分析是一种常用手段.这应该与因子分析等区别开来,重点在于理解主成分分析的作用以及什么情况下使用主成分分析,本文重点讲解如何 ...
- iptables防火墙的原理及应用
简介 (netfilter, 位于Linux内核中的包过滤功能体系 ,称为Linux防火墙的“内核态”) iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防 ...
- Elasticsearch 5.x 字段折叠的使用
在Elasticsearch 5.x 之前,如果实现一个数据折叠的功能是非常复杂的,随着5.X的更新,这一问题变得简单,找到了一遍技术文章,对这个问题描述的非常清楚,收藏下. 参考:https:// ...