bzoj1003题解
【题意分析】
给你一张无向图,固定起点和终点,除这两点外每个点都有可能消失一段时间(保证起点和终点相互可达),每天选择的路径总长,以及对路径的修改都有代价,求给定时间内最小代价保证起点终点始终连通。
【解题思路】
此题数据范围极小,可直接用最短路+DP水过。
先预处理cost[i][j]表示从第i天到第j天都选择一样的路径的最小代价,直接用Dijkstra或SPFA即可。时间复杂度O(N2Mlog2M)或O(N2kE)。
然后DP,f[i]表示从第1天到第i天的最小代价,边界f[0]=-K,转移方程f[i]=f[j]+cost[j+1][i]*(i-j)+K(j<i)。时间复杂度O(N2)。
总时间复杂度O(N2Mlog2M)或O(N2kE)。
【参考代码】
#include <cctype>
#include <cstdio>
#define REP(I,start,end) for(int I=(start);I<=(end);I++)
#define PER(I,start,end) for(int I=(start);I>=(end);I--)
#define maxint 32767
#define maxlongint 2147483647
typedef long long LL;
inline int getint()
{
char ch=getchar();
for(;!isdigit(ch)&&ch!='-';ch=getchar());
bool impositive=ch=='-';
if(impositive)
ch=getchar();
int result=;
for(;isdigit(ch);ch=getchar())
result=(result<<)+(result<<)+ch-'';
return impositive?-result:result;
}
inline LL getLL()
{
char ch=getchar();
for(;!isdigit(ch)&&ch!='-';ch=getchar());
bool impositive=ch=='-';
if(impositive)
ch=getchar();
LL result=0ll;
for(;isdigit(ch);ch=getchar())
result=(result<<)+(result<<)+ch-'';
return impositive?-result:result;
}
template<typename T> inline bool getmax(T &target,T pattern)
{
return pattern>target?target=pattern,true:false;
}
template<typename T> inline bool getmin(T &target,T pattern)
{
return pattern<target?target=pattern,true:false;
}
//Header Template
#include <cstring>
using namespace std;
bool nownot[],used[],cannot[][];
int n,rest[],dist[],f[],map[][],cost[][];
inline int Dijkstra()
{
int cnt=;
REP(i,,n)
if(!nownot[i])
rest[++cnt]=i;
memset(dist,0x3f,sizeof(dist));
memset(used,,sizeof(used));
dist[]=;
REP(i,,cnt)
{
int miner=maxlongint,mini;
REP(j,,cnt)
if(!used[j]&&getmin(miner,dist[j]))
mini=j;
used[mini]=true;
REP(j,,cnt)
if(!used[j])
getmin(dist[j],dist[mini]+map[rest[mini]][rest[j]]);
}
return dist[cnt]<dist[]?dist[cnt]:-;
}
int main()
{
int day=getint();
n=getint();
int K=getint(),m=getint();
memset(map,0x3f,sizeof(map));
while(m--)
{
int u=getint(),v=getint(),l=getint();
map[u][v]=map[v][u]=l;
}
int d=getint();
memset(cannot,,sizeof(cannot));
while(d--)
{
int p=getint(),start=getint(),end=getint();
REP(i,start,end)
cannot[i][p]=true;
}
REP(i,,day)
{
memset(nownot,,sizeof(nownot));
REP(j,i,day)
{
REP(k,,n)
nownot[k]|=cannot[j][k];
cost[i][j]=Dijkstra();
}
}
memset(f,0x7f,sizeof(f));
f[]=-K;
REP(i,,day)
REP(j,,i-)
{
int c=cost[j+][i];
if(c>=)
getmin(f[i],f[j]+cost[j+][i]*(i-j)+K);
}
printf("%d\n",f[day]);
return ;
}
bzoj1003题解的更多相关文章
- 【题解】物流运输 [ZJ2006] [P1772] [BZOJ1003]
[题解]物流运输 [ZJ2006] [P1772] [BZOJ1003] 传送门:物流运输 \([ZJ2006]\) \([P1772]\) \([BZOJ1003]\) [题目描述] 给定一个含 \ ...
- BZOJ1003 物流运输 题解
发现\(n,m\)很小,我们可以先把任意\(2\)天的最短路都给求出来,考虑\(DP\),设\(f[i][j]\)表示\(j+1\)~ \(i\)这几天内走的是最短路线的最优方案,显然最优情况下\(j ...
- 【BZOJ1003】物流运输(动态规划,最短路)
[BZOJ1003]物流运输(动态规划,最短路) 题面 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司 ...
- 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp
「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...
- bzoj1003物流运输 最短路+DP
bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
随机推荐
- Ubuntu下串口工具
一.Kermit 1.安装: sudo apt-get install ckermit 2.配置: sudo gedit /etc/kermit/kermrc 3.在文件末端添加如下内容 : set ...
- leetcood学习笔记-7
Python join()方法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的元素序列. 返回值 返回通过指定字符连接序列中元素后生成的新字符串. ...
- JCF——工具类
- renren-fast-vue-动态路由
在renren-fast-vue项目中,左侧边栏的系统管理这一模块的路由采用的是动态路由的写法, 模块中的路由内容由后台动态生成,在前端开发阶段,采用的是mock模拟数据生成 先是在左侧边栏(view ...
- DELPHI常用类型及定义单元
Controls Application (the variable not a type) Forms Beep SysUtils or Windows (different functions) ...
- Arduino与NodeMCU——联网
我们现在要使用Arduino IDE来配置您的ESP8266芯片.这是使用该芯片的好方法,因为您可以使用着名的Arduino IDE对其进行编程,并重复使用几个现有的Arduino库.如果尚未完成,请 ...
- git_全局忽略DS_store
创建设置文件 vi ~/.gitignore_global 在文件里输入 /*.DS_Store .DS_Store 设置这个文件全局生效 git config --global core.exclu ...
- C/S模式简单socket通信
TCP连接方式 sever.c #include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#inclu ...
- 拾遗:编译安装 vim
在非 Gentoo/Funtoo 系的 Linux 发行版上,软件仓库里的 vim 包可能没有加入 python 特性支持,会造成部分插件无法正常使用 一.下载 vim 源码包 ftp://ftp.v ...
- String、StringBuffer、StringBuilder有什么区别?
1.在字符串不经常发生变化的业务场景优先使用String(代码更清晰简洁).如常量的声明,少量的字符串操作(拼接,删除等). 2.在单线程情况下,如有大量的字符串操作情况,应该使用StringBuil ...