跳到正文

题解

题解 P6029 【[JSOI2010]旅行】

x义x约 4 分钟
原文
文章信息与版本

文章信息

发布
2020年4月13日
收录
2026年4月30日
更新
2026年11月11日
篇幅
约 4 分钟
题目
P6029

版本

  1. v1当前

不难发现我们很难决定一条边要不要换入/换出,也很难得知某条边是否已经被换入。于是我们直接枚举这个要不要换入的标准,具体来说是这样的:

对于一条路径,如果我们希望在它上面释放 dd 次魔法,那么一定是把该路径上最大的 dd 条边和不在该路径上的最小的 dd 条边交换。如果把所有边按权值从小到大排序,看起来大概会是这个样子:

〇〇〇〇〇〇〇〇〇〇〇〇〇\color{red}\text{〇}\color{blue}\text{〇〇}\color{red}\text{〇}\color{blue}\text{〇}\color{black}\text{〇〇}\color{red}\text{〇}\color{black}\text{〇}\color{lightgreen}\text{〇}\color{black}\text{〇}\color{lightgreen}\text{〇〇}

(其中红色代表在路径上而且没被施法,蓝色代表不在路径上但被换入,绿色代表在路径上但被换出)

有一个最长的前缀使得其中的所有边都是红色或蓝色。称它的长度为 LL,这就是我们枚举的这个标准。所有蓝色都小于等于 LL;所有绿色都大于 LL。

f[i][j][k] 表示从 11 走到 ii,路径上使用了 jj 条小于等于 LL 的边(换入不算),kk 次魔法,所有在路径上且大于 LL 且不换出的边的权值和的最小值。跑最短路来 DP。

跑 155 次 2e5 的最短路可能有点困难,所以如果前 LL 条边的长度和已经大于当前答案就可以退出了。

代码如下:

C++
#include<bits/stdc++.h>
using namespace std;

const int maxN=55,maxM=155,maxK=25;
int N,M,K;
int u[maxM],v[maxM],c[maxM],id[maxM];
bool cmp(int a,int b){
	return c[a]<c[b];
}

int lnk[maxN];
int pre[maxM<<1],tgt[maxM<<1],val[maxM<<1],cnt;
void add_E(int u,int v,int c){
	pre[++cnt]=lnk[u],tgt[cnt]=v,val[cnt]=c,lnk[u]=cnt;
}

int L;
struct node{
	int x,y,z,w;
	bool operator <(const node b)const{return w>b.w;}
};
priority_queue<node> Q;
int F[maxN][maxM][maxK];
void Push(node u){
	if(u.y>L||u.z>K) return;
	if(F[u.x][u.y][u.z]<=u.w) return;
	F[u.x][u.y][u.z]=u.w;
	Q.push(u);
}
int ans=0x3f3f3f3f,S;

int main(){
	scanf("%d%d%d",&N,&M,&K);
	for(int i=1;i<=M;i++) scanf("%d%d%d",&u[i],&v[i],&c[i]),id[i]=i;
	sort(id+1,id+M+1,cmp);
	for(int i=1;i<=M;i++) add_E(u[id[i]],v[id[i]],c[id[i]]),add_E(v[id[i]],u[id[i]],c[id[i]]);
	for(L=0;L<=M;L++){
		if(L) S+=c[id[L]];
//		if(S>=ans) break;
		memset(F,63,sizeof(F));
		Push((node){1,0,0,0});
		while(!Q.empty()){
			node u=Q.top();Q.pop();
			if(F[u.x][u.y][u.z]<u.w) continue;
			for(int e=lnk[u.x];e;e=pre[e]){
				if(((e+1)>>1)<=L) Push((node){tgt[e],u.y+1,u.z,u.w});
				else{
					Push((node){tgt[e],u.y,u.z,u.w+val[e]});
					Push((node){tgt[e],u.y,u.z+1,u.w});
				}
			}
		}
		for(int y=0;y<=L;y++)
		for(int z=0;z<=K&&y+z<=L;z++)
			ans=min(ans,F[N][y][z]+S);
	}
	printf("%d\n",ans);
}
原文
存档评论6 条抓取于