ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)
题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路。
分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费。在迪杰斯特拉的dfs过程中,每个结点表示的状态有三个属性:访问至的结点,免费的边数和最小花费。将免费的边数看作层,则该图被分为k层。每次更新除了要对边进行松弛操作,也要对层之间进行松弛操作。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e5+5;
const LL INF =(1ll<<60);
struct Edge{
int to,next;
LL val;
};
struct HeapNode{
LL d; //费用或路径
int u;
bool operator < (const HeapNode & rhs) const{return d > rhs.d;}
};
LL dist[maxn][11];
bool vis[maxn][11];
Edge edges[maxn<<2];
int head[maxn];
struct Dijstra{
int n,m,tot;
int k;
void init(int n,int k){
this->n = n;
this->k = k;
this->tot=0;
memset(head,-1,sizeof(head));
}
void Addedge(int u,int v ,LL dist){
edges[tot].to = v;
edges[tot].val = dist;
edges[tot].next = head[u];
head[u] = tot++;
}
void dijkstra(int s){
memset(vis,0,sizeof(vis));
priority_queue<HeapNode> Q;
memset(dist,0x3f,sizeof(dist));
dist[s][0]=0;
Q.push((HeapNode){0,s});
while(!Q.empty()){
HeapNode x =Q.top(); Q.pop();
int lev = x.u/(n+1);
int u = x.u%(n+1);
if(vis[u][lev]) continue;
vis[u][lev] = 1;
for(int i=head[u];~i;i= edges[i].next){
int v =edges[i].to;
if(dist[u][lev]+edges[i].val<dist[v][lev]){ //同一层中的松弛操作
dist[v][lev] = dist[u][lev] + edges[i].val;
Q.push((HeapNode){dist[v][lev],lev*(n+1)+v});
}
if(lev==k) continue;
if(dist[v][lev+1]>dist[u][lev]){ //往下一层推进的松弛操作
dist[v][lev+1] = dist[u][lev];
Q.push((HeapNode){dist[v][lev+1],(lev+1)*(n+1)+v});
}
}
}
}
}G;
map<int,map<int,LL> > dp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M; scanf("%d",&T);
while(T--){
int k;
scanf("%d %d %d",&N,&M,&k);
G.init(N,k);
dp.clear();
int u,v; LL tmp;
while(M--){
scanf("%d %d %lld",&u,&v,&tmp);
if(!dp[u][v] ||dp[u][v]>tmp){
dp[u][v] = tmp;
}
}
for(int i=1;i<=N;++i){
map<int,LL> ::iterator it;
for(it = dp[i].begin();it!=dp[i].end();++it){
G.Addedge(i,it->first,it->second);
}
}
G.dijkstra(1);
printf("%lld\n",dist[N][k]);
}
return 0;
}
ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)的更多相关文章
- 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
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. 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 (分层dijkstra)
There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)
题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...
- 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 南京赛区网络预赛 L && BZOJ 2763 分层最短路
https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析 分层最短路 我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...
- 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...
随机推荐
- jQuery编程中的一些核心方法简介
调用 jQuery 对象的方法很简单: $('h1').remove(); 大多数 jQuery 方法都是像上面这样被调用的,这些方法都位于 $.fn 命名空间内,这些方法称为 jQuery 对象方法 ...
- iOS开发之--iOS APP打包的时候出现的四个选项
- IOS开发学习笔记039-autolayout 代码实现
本文转载至 http://www.cnblogs.com/songliquan/p/4548206.html 1.代码实现比较复杂 代码实现Autolayout的步骤 利用NSLayoutConstr ...
- 安装PHP扩展-----phpredis
一.redis介绍 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它支持存储的value类型相 ...
- codevs 5964 [SDOI2017]序列计数
[题解] 官方题解就两句话. 写了三个版本的不同分值代码.看代码吧. 前导1 //f[i][j][1/0]表示长为i,sum mod p=j,是否已经选了质数的方案数 #include<cst ...
- Java项目使用oh-my-email发送邮件
本文使用Github开源项目oh-my-email进行测试邮件发送,并未进行更为深度的测试,如果想要快速使用,的确是一个很好的邮件发送组件.https://github.com/biezhi/oh-m ...
- quartz启动Quartz : org.quartz.SchedulerConfigException: Thread count must be > 0
检查quartz.properties数据源配置是否正常
- navicat自动生成DDL语句
场景:当我们在开发库修改表结构之后,需要把这些表结构的变化同步更新到生产库,这时候可以在Navicat中copy表结构变更的SQL语句. 当你点击了“设计表”进行修改表结构,在保存表结构之前点击“SQ ...
- java常用队列分析
一.ArrayBlockingQueue 首先看一段源码: public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...
- How to Design a Good API and Why it Matters
前谷歌首席 Java 架构师谈如何设优秀的 API – 码农网 http://www.codeceo.com/article/google-java-good-api.html 2015-11-24 ...