BZOJ1579 [Usaco2009 Feb]Revamping Trails 道路升级
各种神作不解释QAQQQ
先是写了个作死的spfa本机过了交上去T了。。。
然后不想写Dijkstra各种自暴自弃。。。
最后改了一下步骤加了个SLF过了。。。
首先一个trivial的想法是$dis[p][t]$表示到了$p$号节点,用了$t$次变0技能,然后可以用$dis[q][t] + e[q][p]$和$dis[q][t - 1] + e[q][p] * 0$来更新
然后点数$O(n * k)$,边数$O(m * k)$,再加上usaco硬卡spfa。。。什么最终鬼畜。。。
其实我们可以这样子想:
先把用了$t$次技能的$dis$先算出来,这就是用$dis[q][t] + e[q][p]$更新
然后计算用了$t + 1$次技能,方法就是先用$dis[q]$更新$dis[p]$表示用了一次技能,再跑一边spfa就好了
/**************************************************************
Problem: 1579
User: rausen
Language: C++
Result: Accepted
Time:4312 ms
Memory:3872 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e4 + ;
const int M = 5e4 + ;
const int K = ;
const int inf = 1e9;
const int Maxlen = M * * ; struct edge {
int next, to, v;
edge(int _n = , int _t = , int _v = ) : next(_n), to(_t), v(_v) {}
} e[M << ]; int n, m, k;
int first[N], tot;
int dis[N], tmp[N], v[N]; char buf[Maxlen], *c = buf;
int Len; inline int read(); inline void Add_Edges(int x, int y, int z) {
e[++tot] = edge(first[x], y, z), first[x] = tot;
e[++tot] = edge(first[y], x, z), first[y] = tot;
} #define y e[x].to
void spfa(int S) {
static int i, x, q[], p;
static unsigned short l, r;
for (i = ; i <= n; ++i) dis[i] = i == S ? : inf;
q[l = r = ] = S, v[S] = ;
for (i = ; i <= k + ; ++i) {
while (l != r + ) {
p = q[l++];
for (x = first[p]; x; x = e[x].next)
if (dis[p] + e[x].v < dis[y]) {
dis[y] = dis[p] + e[x].v;
if (!v[y]) {
v[y] = ;
if (dis[y] < dis[q[l]]) q[--l] = y;
else q[++r] = y;
}
}
v[p] = ;
}
if (i == k + ) continue;
for (p = ; p <= n; ++p)
for (tmp[p] = inf, x = first[p]; x; x = e[x].next)
tmp[p] = min(tmp[p], dis[y]);
memcpy(dis, tmp, sizeof(dis));
for (p = ; p <= n; ++p)
if (!v[p]) {
v[p] = ;
if (dis[p] < dis[q[l]]) q[--l] = p;
else q[++r] = p;
}
}
}
#undef y int main() {
Len = fread(c, , Maxlen, stdin);
buf[Len] = '\0';
int i, x, y, z;
n = read(), m = read(), k = read();
for (i = ; i <= m; ++i) {
x = read(), y = read(), z = read();
Add_Edges(x, y, z);
}
spfa();
printf("%d\n", dis[n]);
return ;
} inline int read() {
int x = ;
while (*c < '' || '' < *c) ++c;
while ('' <= *c && *c <= '')
x = x * + *c - '', ++c;
return x;
}
BZOJ1579 [Usaco2009 Feb]Revamping Trails 道路升级的更多相关文章
- [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...
- [BZOJ1579] [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路 + 堆优化dijk)
传送门 dis[i][j]表示第i个点,更新了j次的最短路 此题不良心,卡spfa #include <queue> #include <cstdio> #include &l ...
- 分层图最短路 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M< ...
- Bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 dijkstra,堆,分层图
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1573 Solv ...
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
- 【BZOJ 1579】 1579: [Usaco2009 Feb]Revamping Trails 道路升级 (最短路)
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M< ...
- BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路
BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 -- 分层图最短路
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MB Description 每天,农夫 ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1768 Solv ...
随机推荐
- iOS - UIToolbar
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView <UIBarPositioning& ...
- oracle的正则表达式
阅读目录 1.oracle(regular expression)简单介绍 2.oracle正则特殊字符 3.oracle正则字符簇 4.各种操作符的运算优先级 5.模拟测试例子 6.oracle对应 ...
- Android 自定义对话框
Android实现自定义对话框效果: 核心代码: package com.example.diydialog; import android.os.Bundle; import android.app ...
- strcpy, memcpy, memset函数
一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #inclu ...
- Android网络编程系列 一 TCP/IP协议族
在学习和使用Android网路编程时,我们接触的仅仅是上层协议和接口如Apache的httpclient或者Android自带的httpURlconnection等等.对于这些接口的底层实现我们也有必 ...
- iOS 开发之 Xcode6 installation failed invalid argument!
1.运行模拟器的时候 报出: installation failed invalid argument! 原因分析: 我把Bundle indentifier 置为空了! http://stackov ...
- 转:C 函数调用栈
第一篇: 转自:http://kingj.iteye.com/blog/1555017 本文转自 http://blog.csdn.net/eno_rez/article/details/21586 ...
- python把str转换为int
def str2int(s): def fn(x,y): return x+y def char2num(s): ':9}[s] return reduce(fn,map(char2num,s)) ' ...
- Python核心编程-基础2
open() 和 file() 函数会同时存在, 完成相同的功能.一般说来, 我们建议使用 open() 来读写文件, 在您想说明您在处理文件对象时使用 file() , 例如 if instance ...
- html+asp.net上传文件
type="file" 的name以及id一定要写,并且名字相同 http://niunan.iteye.com/blog/479605 <form id="for ...