题意: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. Python爬虫(五)

    源码: import requests from lxml import etree from my_mysql import MysqlConnect mc = MysqlConnect(','ho ...

  2. thinkphp5如何使用layout

    thinkphp5的layout和以前的版本有点差异. 首先开启配置文件中的 'template' => [      'layout_on' => true,      // 'layo ...

  3. iOS开发之--使用storyboard下,tabbar小图标和文字颜色的设置

    在开发项目的时候,如果是使用故事版设计的架构,那么在设置tabbar小图标的时候,可能会出现一点小问题, 成功的设置方法如下: 1.设置seleectedImage和image,其实就是非选中状态的图 ...

  4. ipc 进程间通讯的AIDL

    1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间 ...

  5. 自定义控件_VIewPager显示多个Item

    一直以来想搞明白这个不完全的VIewPager是怎么做到的,有幸看到这片篇文章 有二种实现方法 1.设置的属性 1.clipChildren属性 2.setPageMargin 3.更新Item外界面 ...

  6. rabbitMq延时消息分级别

    做支付平台的时候.需要实现接受上游支付消息,通知给下游渠道. 针对下游渠道:要实现 按通知次数 递进 延时通知 下游渠道的支付/签约/代扣的状态 可参考微信按照 15/15/30/180/1800/1 ...

  7. Hadoop中正确地添加和移除节点

    正确地添加和移除节点 添加节点 克隆 克隆一台全新的Linux(如有IP冲突,可右击VMware右下角网络图标断开连接) 打开/etc/hostname修改主机名 打开/etc/sysconfig/n ...

  8. (转)Java DecimalFormat 用法(数字格式化)

    我们经常要将数字进行格式化,比如取2位小数,这是最常见的.Java 提供 DecimalFormat 类,帮你用最快的速度将数字格式化为你需要的样子.下面是常用的例子: import java.tex ...

  9. Codeforces Round #427 (Div. 2)—A,B,C,D题

    A. Key races 题目链接:http://codeforces.com/contest/835/problem/A 题目意思:两个比赛打字,每个人有两个参数v和t,v秒表示他打每个字需要多久时 ...

  10. MongoDB 聚合结果大小限制

    The aggregate command can return either a cursor or store the results in a collection. When returnin ...