【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans
预处理cost[a][b] 表示第a天到第b天用同一条线路的成本。
具体转移看代码。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define N 21
typedef long long ll;
#define M 401
int n,m,K,day;
int v[M<<1],first[N],next[M<<1],w[M<<1],en,cost[101][101],f[101];
bool cant[N][101][101];
void AddEdge(int U,int V,int W)
{
v[++en]=V;
w[en]=W;
next[en]=first[U];
first[U]=en;
}
queue<int>q;
bool inq[N];
int dis[N];
void spfa(int from,int to)
{
memset(inq+1,0,sizeof(bool)*n);
memset(dis+1,0x7f,sizeof(int)*n);
q.push(1); inq[1]=1; dis[1]=0;
while(!q.empty())
{
int U=q.front();
for(int i=first[U];i;i=next[i])
if((!cant[v[i]][from][to])&&(ll)dis[v[i]]>(ll)dis[U]+(ll)w[i])
{
dis[v[i]]=dis[U]+w[i];
if(!inq[v[i]])
{
q.push(v[i]);
inq[v[i]]=1;
}
}
q.pop(); inq[U]=0;
}
}
int main()
{
int x,y,z,P;
scanf("%d%d%d%d",&day,&n,&K,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
scanf("%d",&P);
for(;P;--P)
{
scanf("%d%d%d",&x,&y,&z);
for(int i=1;i<=z;++i)
for(int j=max(y,i);j<=day;++j)
cant[x][i][j]=1;
}
for(int i=1;i<=day;++i)
for(int j=i;j<=day;++j)
{
spfa(i,j);
cost[i][j]=dis[n]*(dis[n]>2000000000?1:(j-i+1));
}
memset(f+1,0x7f,sizeof(int)*day);
for(int i=0;i<=day;++i)
for(int j=i+1;j<=day;++j)
f[j]=(int)min((ll)f[j],(ll)f[i]+(ll)(i==0?0:K)+(ll)cost[i+1][j]);
printf("%d\n",f[day]);
return 0;
}
【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans的更多相关文章
- bzoj1003[ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常 ...
- BZOJ1003: [ZJOI2006] 物流运输 trans
物流运输--看了神犇的题解,就是dp+最短路,设f[i]为1~i天的最少花费,那么 dp[i]=min(cost[1,i],min{dp[j]+cost[j+1,i]+K,1≤j<i}) 就是从 ...
- [BZOJ1003] [ZJOI2006] 物流运输trans (最短路 & dp)
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- BZOJ 1003: [ZJOI2006]物流运输trans(最短路+dp)
1A,爽! cost[i][j]表示从第i天到第j天不改路线所需的最小花费,这个可以用最短路预处理出.然后dp(i)=cost[j][i]+dp(j-1)+c. c为该路线的花费. --------- ...
- BZOJP1003 [ZJOI2006]物流运输trans
BZOJP1003 [ZJOI2006]物流运输trans 1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MB Sub ...
- BZOJ 1003 [ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4242 Solved: 1765[Submit] ...
- bzoj1003: [ZJOI2006]物流运输(DP+spfa)
1003: [ZJOI2006]物流运输 题目:传送门 题解: 可以用spfa处理出第i天到第j都走这条路的花费,记录为cost f[i]表示前i天的最小花费:f[i]=min(f[i],f[j-1] ...
- bzoj1003 [ZJOI2006]物流运输
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6300 Solved: 2597[Submit][Stat ...
- [bzoj1003][ZJOI2006][物流运输] (最短路+dp)
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
随机推荐
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C (用map 超时)
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 配置Tomcat时server.xml和content.xml自动还原问题
当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content.xml文件. 但是当我们修改完后重启Tomcat服务器时发现xml文件又被还原了,修改无效果. 为 ...
- Linux下rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- Asp.Net Core 基于QuartzNet任务管理系统(这是一篇用来水的随笔)
之前一直想搞个后台任务管理系统,零零散散的搞到现在,也算完成了. 废话不多说,进入正题. github地址:https://github.com/YANGKANG01/QuartzNetJob 一.项 ...
- HDU 5685 Problem A | 快速幂+逆元
Problem A Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Spring发展史
https://www.cnblogs.com/RunForLove/p/4641672.html
- 【BZOJ1996】【HNOI2010】合唱队 [区间DP]
合唱队 Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Sample ...
- POJ 2395 Out of Hay (prim)
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...
- C++11 lambda函数符
#include<iostream> #include<vector> #include<algorithm> #include<cmath> #inc ...
- linux进程的休眠(等待队列)【转】
转自:http://www.cnblogs.com/noaming1900/archive/2011/01/14/1935526.html (转载) bojan 收录于2010-10-09 阅读数: ...