题意: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 (分层迪杰斯特拉)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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, ...

  4. 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). ...

  5. ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)

    题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...

  6. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)

    题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...

  8. ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路

    https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析  分层最短路  我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...

  9. 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...

随机推荐

  1. jQuery的end()方法使用详解

    end()方法的定义和用法: end()方法能够回到最近的一个"破坏性"操作之前,即将匹配的元素列表变为前一次的状态.如果没有破坏性操作将返回一个空集.破坏性操作的概念:指任何改变 ...

  2. MyEclipse10.6 安装SVN插件方法及插件下载地址

        今天MyEclipse10.6出了点问题,所以重装了它,同一时候也把svn的插件重装了一次,把网上资源和自己的经历顺便在博客这里记录一下.建议直接看方法一好了,简单方便,不必要折腾太多. 下来 ...

  3. Linux命令之乐--test

    官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...

  4. iOS 7 SDK: 如何使用后台获取(Background Fetch)

    本文转载至 http://www.cocoachina.com/applenews/devnews/2013/1114/7350.html 本文主要教你如何使用iOS 7 SDK多任务处理API--B ...

  5. JSP页面的生命周期

    JSP页面的生命周期:我们假设要访问的jsp页面是index.jsp.首先,用户发出请求index.jsp:服务器会判断是否是第一次请求:如果是的话,JSP引擎会把该JSP文件转换成为一个Servle ...

  6. poj_1836 动态规划

    题目大意 N个士兵排成一排,不是按照高度顺序排列.现在想要从中去掉几名士兵,从而使得队伍中剩余的士兵能够看到这排最左边或者最右边的那个士兵,某士兵能够看到最左边(或最右边)的士兵指这名士兵和最左边(或 ...

  7. oracle11g安装完成后修改字符集

    author : headsen chen date:2018-05-10  10:27:16 oracle11g完成安装后,由于默认安装的时候无法指定字符集,所以手动修改字符集和10g版本一样的字符 ...

  8. centos7上开启路由转发

    CentOS7 开启路由转发 2018-03-27   09:18:14 1.临时开启,(写入内存,在内存中开启) echo "1" > /proc/sys/net/ipv4 ...

  9. PHP mysql基本语句指令

    /*选择数据库 use test; */ /* 显示所有的数据库 show databases; */ /*删除表/数据库 drop database test1; delete from user1 ...

  10. VUE学习总结

    VUE学习总结 文档:https://cn.vuejs.org/v2/guide/ Webstorm的一些常用快捷键:1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任 ...