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 ...
随机推荐
- 非关系型数据库MongoDB入门
本文分为以下四块简单介绍非关系型数据库MongoDB:1.MongoDB简介.2.MongoDB和关系数据库对比.3.MongoDB基本概念.4.mongo shell的使用以及对MongoDB的增删 ...
- informix 计算 日期之差
原文地址:http://blog.chinaunix.net/uid-678894-id-3138829.html https://blog.csdn.net/zhengqiqiqinqin/arti ...
- war包里面文件的修改方式
1 将war包移动到一个干净的路径下,使用 jar xvf ROOT.war 命令将war进行解压操作 2 修改相应的文件内容,修改想要修改的文件,比如web.xml 3 使用 j ...
- leetcood学习笔记-26-删除排序数组中的重复项
题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums) -> int: for i in range(len(nums)-1, ...
- python3 获取电脑磁盘、CPU、内存使用情况
import psutil # cd C:\Python36-32\Scripts pip install psutil # 获取本机磁盘使用率和剩余空间G信息 def get_disk_info() ...
- 二:unittest框架配合selenium之xpath定位
刚开始学习selenium自动化测试时,犯了一个不该犯的错误,偷懒,使用火狐浏览器中的扩展FIREBUG,FIREPATH来辅助定位. 虽然用的定位方法大多数是使用XPATH方法,但是是工具定位出来的 ...
- 用 Flask 来写个轻博客 (2) — Hello World!
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现最简单的 Flask 应用 创建 config.py 文 ...
- [经典]Python 一篇学会多线程
对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的MS-DOS时代,操作系统处理问题都是单任 ...
- 3、加强siege性能测试
需求1:使用siege对多个页面进行加压 1.对如下3个页面加压 http://jrjf.hscx365.com/ http://jrjf.hscx365.com/solution/armyone.h ...
- linux get current thread count and system threads limit
get current thread count grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print su ...