[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(nT^3)\).\(T^3\)很难优化,考虑怎么优化\(n\)

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

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值