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 ...
随机推荐
- vue 点击切换图标
<div @click="showImg" class="showSearch"> <img class="header_img&q ...
- 启动AutoCAD时自动加载.NET开发的DLL
程序组织,建立名为*.bundle的文件夹,创建Contents子文件夹,并将dll,ico等文件放进Contents中,在*.bundle中创建PackageContents.xml文件,内容如下: ...
- Delphi 打印纸张选项设置参数
{ paper selections } {$EXTERNALSYM DMPAPER_LETTER} DMPAPER_LETTER = 1; { Letter 8 12 x 11 in } {$EXT ...
- Linux系统上安装MySQL 5.5prm
http://www.cnblogs.com/sunson/articles/2172086.html
- js设计模式——6.模板方法模式与职责链模式
js设计模式——6.模板方法模式与职责链模式 职责链模式
- [Go语言]cgo用法演示
经历了数十年发展的C语言,各种各样的现成的库已经非常丰富.通过cgo,可以在Go语言中使用C语言代码,充分利用好现有的“轮子”. 本文所有代码,在下述环境中调试通过: Windows 8.1 ...
- java 原生 HttpClient
package org.rx.socks.http; import com.google.common.base.Strings; import lombok.SneakyThrows; import ...
- C语言中的数据类型转换函数
头文件#include<stdlib.h> 1. 函数名: atof 功 能: 把字符串转换成浮点数 用 法: double atof(const char *nptr); 2.函数名: ...
- CSS:CSS 简介
ylbtech-CSS:CSS 简介 1.返回顶部 1. CSS 简介 你需要具备的知识 在继续学习之前,你需要对下面的知识有基本的了解: HTML / XHTML 如果你希望首先学习这些项目,请在 ...
- Django框架(九)—— 单表增删改查,在Python脚本中调用Django环境
目录 单表增删改查,在Python脚本中调用Django环境 一.数据库连接配置 二.orm创建表和字段 三.单表增删改查 1.增加数据 2.删除数据 3.修改数据 4.查询数据 四.在Python脚 ...