[USACO2007NOVG] Cow Relays G

题目描述

For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.

To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

输入格式

* Line 1: Four space-separated integers: N, T, S, and E

* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

输出格式

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

样例 #1

样例输入 #1

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

样例输出 #1

10

有T条边连起来的图最多T+1个点,可以对所有点进行离散化。点的个数是T级别的。

首先有一个暴力的思路就是循环n次Floyd,然后输出s到e的距离。复杂度O(nT3).T3很难优化,考虑怎么优化n

发现Floyd的过程似乎是可以通过倍增来实现的。首先预处理出在n=2i时任意两点之间的距离,然后把n拆成二进制数一个一个松弛上去。复杂度O(T3logn),可以通过此题。

#include<bits/stdc++.h>
using namespace std;
int n,t,s,e,u[105],v[105],w[105],m,dp[25][205][205],f[2][205][205],c,lsh[205],fa[1005],x,y;
int find(int x)
{
	if(fa[x]==x)
		return x;
	return fa[x]=find(fa[x]);
}
int main()
{
	memset(dp,0x3f,sizeof(dp));
	memset(f,0x3f,sizeof(f)); 
	scanf("%d%d%d%d",&t,&m,&s,&e);
	for(int i=1;i<=1000;i++)
		fa[i]=i;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",w+i,u+i,v+i);
		fa[find(u[i])]=find(v[i]);
	}
	for(int i=1;i<=1000;i++)
		if(find(i)==find(s))
			lsh[++n]=i;	
	for(int i=1;i<=n;i++)
		f[1][i][i]=0;
	for(int i=1;i<=m;i++)
	{
		x=lower_bound(lsh+1,lsh+n+1,u[i])-lsh;
		y=lower_bound(lsh+1,lsh+n+1,v[i])-lsh;
		if(lsh[x]==u[i]&&lsh[y]==v[i])
			dp[0][x][y]=dp[0][y][x]=min(dp[0][x][y],w[i]);
	}
	for(int p=1;(1<<p)<=t;p++)
	{
		for(int k=1;k<=n;k++)
		{
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=n;j++)
				{
					dp[p][i][j]=min(dp[p][i][j],dp[p-1][i][k]+dp[p-1][k][j]);
				}
			}
		}
	}
	for(int p=0;(1<<p)<=t;p++)
	{
		if(t&(1<<p))
		{
			for(int k=1;k<=n;k++)
			{
				for(int i=1;i<=n;i++)
				{
					for(int j=1;j<=n;j++)
					{
						f[c][i][j]=min(f[c][i][j],f[!c][i][k]+dp[p][k][j]);
					}
				}
			}
			c^=1;
			memset(f[c],0x3f,sizeof(f[c]));
		}
	}
	printf("%d",f[!c][lower_bound(lsh+1,lsh+n+1,s)-lsh][lower_bound(lsh+1,lsh+n+1,e)-lsh]);
	return 0;
}
posted @   灰鲭鲨  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示