【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
定义dis[i][j]表示到达i这个点。
用掉了j次去除边的机会的最短路。
dis[1][0]= 0;
在写松弛条件的时候。
如果用了去除边的机会。
就把机会+1再更新最短路就好。
用spfa会超时。
写个dijkstra+优先队列优化就ok
在dis[n][0..k]中找最小值就好。
【代码】
#include <bits/stdc++.h>
#define LL long long
#define ms(a,b) memset(a,b,sizeof a)
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
const LL mod=1000000007;
LL powmod(LL a,LL b) {LL res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL gcd(LL a,LL b) { return b?gcd(b,a%b):a;}
// head
using namespace std;
const int N = 1e5;
const int K = 10;
const int M = 2e5;
struct abc
{
int en,w,nex;
}bian[M+10];
int n,m,k,fir[N+10],totm;
LL dis[N+10][K+5];
multiset< pair<LL,pair<int,int> > > myset;
multiset< pair<LL,pair<int,int> > >::iterator it;
int main(){
while (n>0){
n/=2;
n++;
n--;
n/=2;
}
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
memset(fir,255,sizeof fir);
totm = 0;
cin >> n >> m >> k;
rep1(i,1,m){
int x,y,z;
cin >> x >> y >> z;
totm++;
bian[totm].nex = fir[x];
fir[x] = totm;
bian[totm].en = y;
bian[totm].w = z;
}
memset(dis,255,sizeof dis);
dis[1][0] = 0;
myset.insert(make_pair(0,make_pair(1,0)));
while (!myset.empty()){
pair<LL,pair<int,int> > temp = (*myset.begin());
myset.erase(myset.begin());
int x = temp.second.first,times = temp.second.second;
for (int i = fir[x];i!=-1;i = bian[i].nex)
{
int y = bian[i].en;LL w = bian[i].w;
if (dis[y][times]==-1 || dis[y][times]>dis[x][times]+w){
if (dis[y][times]!=-1){
it = myset.find(make_pair(dis[y][times],make_pair(y,times)));
myset.erase(it);
}
dis[y][times] = dis[x][times]+w;
myset.insert(make_pair(dis[y][times],make_pair(y,times)));
}
if (times+1<=k && ( dis[y][times+1]==-1 || dis[y][times+1]>dis[x][times]))
{
if (dis[y][times+1]!=-1)
{
it = myset.find(make_pair(dis[y][times+1],make_pair(y,times+1)));
myset.erase(it);
}
dis[y][times+1] = dis[x][times];
myset.insert(make_pair(dis[y][times+1],make_pair(y,times+1)));
}
}
}
LL mi = dis[n][0];
for (int i = 1;i <= k;i++)
if (dis[n][i]!=-1){
mi = min(mi,dis[n][i]);
}
cout<<mi<<endl;
}
return 0;
}
【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze的更多相关文章
- 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 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. 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层转 ...
随机推荐
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 检測wifi是否须要portal验证 公共场所wifi验证
何为wifi portal验证? 平时在商场,咖啡厅,银行等公共场所.我们手机提示:有可用WLAN.这些WIFI能够直接连接,不须要password,但须要我们手动在手机网页上进行验证,通常是输入一个 ...
- Linux学习笔记:什么是x86
什么是x86 和硬件打交道常常会听说x86,疑惑的时候自己翻过书上网查过资料.可是都不甚明白.近期再次遇到x86这个词,随具体了解并做笔记记录. 想要知道什么是x86应该先区分CPU的分类. CPU ...
- 安装10gR2的硬件要求
1.至少1G的RAM. 2.RAM与swap关系: RAM swap 512M以上 2*RAM (非常奇怪.至少1G的RAM.还写512的 ...
- Android之怎样更改获取焦点的先后顺序
在组件中增加<requestFocus />能够首先获得焦点 以TextView为例: 例如以下: <TextView android:layout_width=&q ...
- 编写MyLayer,2 锚点,3 精灵的创建,4 zorder
1 编写MyLayer 头文件:MyLayer.h #include "cocos2d.h" USING_NS_CC; //代表的是: using namespace c ...
- ubuntu清华源【转】
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 可以选择ubuntu的版本更新源.
- global cache cr request
当一个进程访问需要一个或者多个块时,它会首先检查自己的CACHE是否存在该块,如果发现没有,就会先通过global cache赋予这些块 共享访问的权限,然后再访问.假如,通过global cache ...
- Node.js:JXcore
ylbtech-Node.js:JXcore 1.返回顶部 1. Node.js JXcore 打包 Node.js 是一个开放源代码.跨平台的.用于服务器端和网络应用的运行环境. JXcore 是一 ...
- C# WinForm的练习
今天写了一个WinForm的练习,将源代码贴出来和大家一起学习学习. 首先:按照下图将一个button控件.三个RadioButton控件.三个CheckBox控件.一个Label控件和一个Track ...