usaco3.2.6 Sweet Butter

本文介绍了一个关于寻找最优路径的问题——如何在牧场中放置糖立方以最小化所有奶牛走到糖立方位置的总距离。通过使用堆优化的Dijkstra算法解决该问题,并详细展示了算法实现过程及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 原题

Sweet Butter

Greg Galperin -- 2001

Farmer John has discovered the secret to making the sweetest butter in all of Wisconsin: sugar. By placing a sugar cube out in the pastures, he knows the N (1 <= N <= 500) cows will lick it and thus will produce super-sweet butter which can be marketed at better prices. Of course, he spends the extra money on luxuries for the cows.

FJ is a sly farmer. Like Pavlov of old, he knows he can train the cows to go to a certain pasture when they hear a bell. He intends to put the sugar there and then ring the bell in the middle of the afternoon so that the evening's milking produces perfect milk.

FJ knows each cow spends her time in a given pasture (not necessarily alone). Given the pasture location of the cows and a description of the paths that connect the pastures, find the pasture in which to place the sugar cube so that the total distance walked by the cows when FJ rings the bell is minimized. FJ knows the fields are connected well enough that some solution is always possible.

PROGRAM NAME: butter

INPUT FORMAT

  • Line 1: Three space-separated integers: N, the number of pastures: P (2 <= P <= 800), and the number of connecting paths: C (1 <= C <= 1,450). Cows are uniquely numbered 1..N. Pastures are uniquely numbered 1..P.
  • Lines 2..N+1: Each line contains a single integer that is the pasture number in which a cow is grazing. Cow i's pasture is listed on line i+1.
  • Lines N+2..N+C+1: Each line contains three space-separated integers that describe a single path that connects a pair of pastures and its length. Paths may be traversed in either direction. No pair of pastures is directly connected by more than one path. The first two integers are in the range 1..P; the third integer is in the range (1..225).

SAMPLE INPUT (file butter.in)

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

INPUT DETAILS

This diagram shows the connections geometrically:
          P2  
 P1 @--1--@ C1
     \    |\
      \   | \
       5  7  3
        \ |   \
         \|    \ C3
       C2 @--5--@
          P3    P4

OUTPUT FORMAT

  • Line 1: A single integer that is the minimum distance the cows must walk to a pasture with a sugar cube.

SAMPLE OUTPUT (file butter.out)

8

OUTPUT DETAILS:

Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5
units; cow 3 walks 0 units -- a total of 8.



二 分析

floyd算法的复杂度是O(p^3),会TLE(其实这题p最大才800,不是很明白为啥10^8级会超时)。于是用堆优化的dijkstra来做,复杂度会降到O(p*p*lg(p) + p*c)。但第一次写蠢了:dijkstra找到当前最近点的复杂度是降到了O(lg(p))了,但是更新的复杂度用邻接矩阵写的话还是O(p),整体复杂度还是O(p^3)。。改成邻接表就过了。


三 代码

运行结果(dijkstra堆优化):
USER: Qi Shen [maxkibb3]
TASK: butter
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 6700 KB]
   Test 2: TEST OK [0.000 secs, 6700 KB]
   Test 3: TEST OK [0.000 secs, 6700 KB]
   Test 4: TEST OK [0.000 secs, 6700 KB]
   Test 5: TEST OK [0.011 secs, 6700 KB]
   Test 6: TEST OK [0.022 secs, 6700 KB]
   Test 7: TEST OK [0.065 secs, 6700 KB]
   Test 8: TEST OK [0.130 secs, 6700 KB]
   Test 9: TEST OK [0.194 secs, 6700 KB]
   Test 10: TEST OK [0.184 secs, 6700 KB]

All tests OK.

Your program ('butter') produced all correct answers! This is your submission #4 for this problem. Congratulations!


AC代码(dijkstra堆优化):
/*
ID:maxkibb3
LANG:C++
PROG:butter
*/

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

const int MAX_P = 801;
const int INF = 99999999;

struct Node {
	int idx;
	int d;
	bool operator < (const Node &obj) const {
		if(d == obj.d) return idx < obj.idx;
		return d > obj.d;
	}
};

int n, p, c;
int cow[MAX_P];
vector<Node> dis[MAX_P];
int short_dis[MAX_P][MAX_P];
int ans = INF;

Node node(int _idx, int _d) {
	Node ret;
	ret.idx = _idx;
	ret.d = _d;
	return ret;
}

void dijkstra(int s) {
	for(int i = 1; i <= p; i++) {
		short_dis[s][i] = INF;
	}
	Node head = node(s, 0);
	priority_queue<Node> q;
	q.push(head);
	while(!q.empty()) {
		head = q.top();
		q.pop();
		if(head.d > short_dis[s][head.idx]) continue;
		else short_dis[s][head.idx] = head.d;
		for(int i = 0; i < dis[head.idx].size(); i++) {
			int t = dis[head.idx][i].idx;
			if(short_dis[s][t] > head.d + dis[head.idx][i].d) {
				short_dis[s][t] = head.d + dis[head.idx][i].d;
				Node new_ele = node(t, short_dis[s][t]);
				q.push(new_ele);
			}
		}
	}
}

int main() {
	freopen("butter.in", "r", stdin);
	freopen("butter.out", "w", stdout);
	scanf("%d%d%d", &n, &p, &c);
	for(int i = 0; i < n; i++) {
		scanf("%d", &cow[i]);
	}
	for(int i = 0; i < c; i++) {
		int s, e, len;
		scanf("%d%d%d", &s, &e, &len);
		dis[s].push_back(node(e, len));
		dis[e].push_back(node(s, len));
	}
	
	for(int i = 1; i <= p; i++) {
		dijkstra(i);
	}
	
	for(int i = 1; i <= p; i++) {
		int candidate = 0;
		for(int j = 0; j < n; j++) {
			candidate += short_dis[cow[j]][i];
		}
		if(candidate < ans) {
			ans = candidate;
		}
	}
	printf("%d\n", ans);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值