一本通1490:秘密的牛奶运输

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+7,M = 3e5 + 7,MM = 3e5+7;
const ll INF = 0x7ffffffffff;;
int n,m;
ll sum;
int cnt,head[MM],ver[MM],nex[MM],edge[MM];
int tree[MM],pre[N],ppre[N][23],depth[N],lg[N];
ll maxf[N][23],minf[N][23];//这两个要和ans 作比较,所以用ll 
struct E{
	int from,to,w;
	E(){}
	E(int from,int to,int w) : from(from),to(to),w(w){}
	bool operator < (const E &b)const{
		return w < b.w;
	}
}e[M];
void add(int x,int y,int w){
	 ver[++cnt] = y;
	 nex[cnt] = head[x];
	 edge[cnt] = w;
	 head[x] = cnt;
}
int find(int x){
	return x == pre[x] ? x : pre[x] = find(pre[x]);
}
void read(){
	scanf("%d%d",&n,&m);
	for(int i=1;i <= m;i ++)
		scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w);
	
	for(int i=0; i < N; i++)
	pre[i] = i;
}
void work1(){
	sort(e+1,e+m+1);
	for(int i=1;i <= m;i ++){
		int x = e[i].from,y = e[i].to,w = e[i].w;
		int fx = find(x), fy = find(y);
		if(fx != fy){
			pre[fx] = fy;
			sum += w;
			add(x,y,w);
			add(y,x,w);
			tree[i] = 1;
		}
	}
}
void dfs(int f,int fa,int w){
	depth[f] = depth[fa] + 1;
	ppre[f][0] = fa;
	minf[f][0] = -INF;
	maxf[f][0] = w;
	for(int i=1; (1<<i) <= depth[f];i++){
		ppre[f][i] = ppre[ppre[f][i-1]][i-1];
		maxf[f][i] = max(maxf[f][i-1],maxf[ppre[f][i-1]][i-1]);
		minf[f][i] = max(minf[f][i-1],minf[ppre[f][i-1]][i-1]);//这里分清次小关系 
		if(maxf[f][i-1] > maxf[ppre[f][i-1]][i-1]) minf[f][i]  = max(minf[f][i],maxf[ppre[f][i-1]][i-1]);
		else if(maxf[f][i-1] < maxf[ppre[f][i-1]][i-1]) minf[f][i] = max(minf[f][i],maxf[f][i-1]);
	}
	
	for(int i=head[f]; i ; i=nex[i]){
		int y = ver[i],w = edge[i];
		if(y != fa){
			dfs(y,f,w);
		}
	}
}
int lca(int x,int y){
	if(depth[x] < depth[y]) swap(x,y);
	while(depth[x] > depth[y]) 
	x = ppre[x][lg[depth[x]-depth[y]] -1];
	
	if(x==y) return x;
	for(int i = lg[depth[x]]-1; i>=0; i--){
		if(ppre[x][i] != ppre[y][i]) 
		x= ppre[x][i],y = ppre[y][i];
	}
		
	return ppre[x][0];
}
ll qmax(int x,int y,int maxx){
	ll ans = -INF;
	
	for(int i = lg[depth[x]]-1;i>=0;i--){
		if(depth[ppre[x][i]]>=depth[y]){
			if(maxx != maxf[x][i]) ans = max(ans,maxf[x][i]);
			else ans = max(ans,minf[x][i]);
			x = ppre[x][i];
		}
	}
	return ans;
} 
void work2(){
	for(int i=1;i <= n;i++)
	lg[i] = lg[i-1] + (1<<lg[i-1]==i);

	dfs(1,0,0);
	ll ans = INF;
	for(int i = 1;i <= m;i++){
		if(tree[i]) continue;
		int x = e[i].from,y = e[i].to,w = e[i].w;
		int lc = lca(x,y);
		ll maxx = qmax(x,lc,w);
		ll maxv = qmax(y,lc,w);
		ans = min(ans,sum-max(maxx,maxv)+w);//找到到公共父亲节点路径中最大的那条,替换到非树边。 
	}
	printf("%lld\n",ans);
}
int main(){
	read();
	work1();
	work2();
	return 0;
}

信息学奥赛一本1255:迷宫问题是一个关于迷宫的问题。这个问题要求过广搜算法来解决迷宫问题,找到走出迷宫的路径。具体来说,迷宫可以看成是由n×n的格点组成,每个格点只有两种状态, "." 和 "#" 。其中 "." 代表可行的路径,"#" 代表不可行的墙壁。过广搜算法,我们可以搜索从起点到终点的路径,找到一条合法的路径即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [信息学奥赛一本 1255:迷宫问题 | OpenJudge NOI 2.5 7084:迷宫问题](https://blog.csdn.net/lq1990717/article/details/124721407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [c++信息学奥赛一本1215题解](https://download.csdn.net/download/Asad_Yuen/87357807)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [信息学奥赛一本(1255:迷宫问题)](https://blog.csdn.net/lvcheng0309/article/details/118879231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值