USACO section 3.2 Sweet Butter(SPFA)

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情

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 the 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.


 

 

思路:用SPFA算出所有牧场到糖所在在的牧场的最短距离。在枚举所有糖可以在的牧场,算出奶牛所在牧场的最小距离加和就是答案

 

代码

/*
ID:nealgav1
LANG:C++
PROG:butter
*/
#include<fstream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
ifstream cin("butter.in");
ofstream cout("butter.out");
const int oo=1e9;
const int mm=1455;
class node{public:int v,w;};
vector<node>edge[mm];
bool vis[mm];///判重
int cow[mm],dist[mm];
void SPFA(int u)///求出所有牧场到u牧场的最短距离
{ int v,w;
  queue<int>Q;
  vector<node>::iterator it;
  memset(vis,0,sizeof(vis));
  dist[u]=0;
  Q.push(u);vis[u]=true;
  while(!Q.empty())
  { u=Q.front();
    for(it=edge[u].begin();it!=edge[u].end();it++)
    { v=it->v;w=it->w;
      if(dist[u]<dist[v]-w)
      {dist[v]=dist[u]+w;
       if(!vis[v])Q.push(v);
       vis[v]=true;
      }
    }
    vis[u]=false;
    Q.pop();
  }
}
int main()
{
  int Cow,Pas,Path;
  int u,v,w;
  node t;
  cin>>Cow>>Pas>>Path;
  for(int i=0;i<Cow;i++)cin>>cow[i];
  for(int i=1;i<=Pas;i++)edge[i].clear();
  for(int i=0;i<Path;i++)
  {
    cin>>u>>v>>w;
    t.v=v;t.w=w;
    edge[u].push_back(t);
    t.v=u;edge[v].push_back(t);
  }
  int ans=oo;
  for(int i=1;i<=Pas;i++)
  { int num=0;
    for(int j=1;j<=Pas;j++)dist[j]=oo;
    SPFA(i);///将所有奶牛所在牧场的距离加和,求最小就是答案
    for(int j=0;j<Cow;j++)num+=dist[cow[j]];
    if(num<ans)ans=num;
  }
  cout<<ans<<"\n";
  cin.close();cout.close();
}


 

 

 

 

Sweet Butter
Alex Schwendner

We approach this problem directly, by calculating the distance from each cow to each pasture. Once this is done, we will simply have to sum the distances for each cow to get the total cost of putting the sugar cube at a given pasture. The key to a fast distance calculation is that our graph is quite sparse. Thus, we use Dijkstra with a heap to calculate the distance from a given cow to all pastures. This requires on the order of N*C*log(P), or about 7,000,000, operations.

#include <stdio.h>
#include <string.h>

const int BIG = 1000000000;

const int MAXV = 800;
const int MAXC = 500;
const int MAXE = 1450;


int cows;
int v,e;


int cow_pos[MAXC];
int degree[MAXV];
int con[MAXV][MAXV];
int cost[MAXV][MAXV];

int dist[MAXC][MAXV];


int heapsize;
int heap_id[MAXV];
int heap_val[MAXV];
int heap_lookup[MAXV];


bool validheap(void){
  for(int i = 0; i < heapsize; ++i){
    if(!(0 <= heap_id[i] && heap_id[i] < v)){
      return(false);
    }
    if(heap_lookup[heap_id[i]] != i){
      return(false);
    }
  }
  return(true);
}


void heap_swap(int i, int j){
  int s;

  s = heap_val[i];
  heap_val[i] = heap_val[j];
  heap_val[j] = s;

  heap_lookup[heap_id[i]] = j;

  heap_lookup[heap_id[j]] = i;

  s = heap_id[i];
  heap_id[i] = heap_id[j];
  heap_id[j] = s;

}


void heap_up(int i){
  if(i > 0 && heap_val[(i-1) / 2] > heap_val[i]){
    heap_swap(i, (i-1)/2);
    heap_up((i-1)/2);
  }
}


void heap_down(int i){
  int a = 2*i+1;
  int b = 2*i+2;

  if(b < heapsize){
    if(heap_val[b] < heap_val[a] && heap_val[b] < heap_val[i]){
      heap_swap(i, b);
      heap_down(b);
      return;
    }
  }
  if(a < heapsize && heap_val[a] < heap_val[i]){
    heap_swap(i, a);
    heap_down(a);
  }
}




int main(){


  FILE *filein = fopen("butter.in", "r");
  fscanf(filein, "%d %d %d", &cows, &v, &e);
  for(int i = 0; i < cows; ++i){
    fscanf(filein, "%d", &cow_pos[i]);
    --cow_pos[i];
  }
  for(int i = 0; i < v; ++i){
    degree[i] = 0;
  }
  for(int i = 0; i < e; ++i){
    int a,b,c;
    fscanf(filein, "%d %d %d", &a, &b, &c);
    --a;
    --b;

    con[a][degree[a]] = b;
    cost[a][degree[a]] = c;
    ++degree[a];

    con[b][degree[b]] = a;
    cost[b][degree[b]] = c;
    ++degree[b];

  }
  fclose(filein);


  for(int i = 0; i < cows; ++i){
    heapsize = v;
    for(int j = 0; j < v; ++j){
      heap_id[j] = j;
      heap_val[j] = BIG;
      heap_lookup[j] = j;
    }
    heap_val[cow_pos[i]] = 0;
    heap_up(cow_pos[i]);

    bool fixed[MAXV];
    memset(fixed, false, v);
    for(int j = 0; j < v; ++j){
      int p = heap_id[0];
      dist[i][p] = heap_val[0];
      fixed[p] = true;
      heap_swap(0, heapsize-1);
      --heapsize;
      heap_down(0);

      for(int k = 0; k < degree[p]; ++k){
	int q = con[p][k];
	if(!fixed[q]){
	  if(heap_val[heap_lookup[q]] > dist[i][p] + cost[p][k]){
	    heap_val[heap_lookup[q]] = dist[i][p] + cost[p][k];
	    heap_up(heap_lookup[q]);
	  }
	}
      }

    }
  }

  int best = BIG;
  for(int i = 0; i < v; ++i){
    int total = 0;
    for(int j = 0; j < cows; ++j){
      total += dist[j][i];
    }
    best <?= total;
  }


  FILE *fileout = fopen("butter.out", "w");
  fprintf(fileout, "%d\n", best);
  fclose(fileout);


  return(0);
}

 

【网络协议】TCP的拥塞控制机制 前言 计算机网络中的带宽、交换节点中的缓存和处理机等,都是网络的资源,在某段时间内,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏,这种情况就叫做拥塞。 所谓拥塞控制,就是防止过多的数据注入到网络中,从而使网络中的路由器或链路不致过载。要注意用拥塞控制与流量控制的区别,拥塞控制是一个全局性的过程,涉及到所有的额主机、路由器,以及与降低网 拥塞控制的算法有:慢开始、拥塞避免、快重传、快恢复四种。 慢开始和拥塞避免 发送方维持一个拥塞窗口的状态变量,其大小取决于网络的拥塞程度 阅读详情

相关推荐

MATLAB并行开不了的解决办法

在服务器上新安装了MATLAB 2013b,在开并行计算的时候出现了下列问题 经过在网上搜多这个问题,我感觉这个问题应该是安装的时候的验证问题或者激活不成功之类的。注意其中有一句validate the profile ‘local’ in the Cluster Profile Manager.在matlab官网上找到了设置Cluster Manager的教程,如下 parallel>

sinat_36713319的博客 5987

USACO Section 3.2 Sweet Butter(SPFA)

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 ...

angtongyou1893的博客 204

别再只调电压了!深入理解数控电源的CC/CV模式与双闭环PID控制(以BUCK电路为例)

本文深入解析数控电源的CC/CV模式与双闭环PID控制技术,以BUCK电路为例,探讨如何通过双闭环控制优化负载突变响应和模式切换稳定性。文章详细介绍了电压环与电流环的协同工作原理、PID参数整定技巧及工程实践中的非线性补偿方法,帮助工程师提升电源设计的智能控制能力。

weixin_27017211的博客 264

USACO Section 3.2 Sweet Butter - 还是SPFA好使..

这道题就是先找出两两间的最短路...然后扫描所有点找出到达所要求点距离和最小的..    开始我用的Floyd..超时很严重阿.然后想写Bellman-Ford...但发现效率也不高...那就果断SPFA了...果然高效...水过... Progarm: /* ID: zzyzzy12 LANG: C++ TASK: butter */ #include

zzyzzy12 1126

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1606

搭建PX4开发系统(Ubuntu 22.04)

本文是《从零开始学 PX4 无人机开发》系列第二篇,适合开发者在 Ubuntu 系统下快速搭建 PX4 编译与仿真环境,支持 QGroundControl、Gazebo、MAVSDK 等完整工具链。

qq_15856721的博客 4200

USACO Training Section 3.2 Sweet Butter

[url="http://ace.delos.com/usacoprob2?a=KxExhYBcUZv&S=butter"]英文原题[/url] [url="http://www.wzoi.org/usaco/main.asp?dir=15&wenjian=611"]中文题译[/url] 大意:农场之间有路构成稀疏无向图,若干奶牛分散在不同农场,要让奶牛到某个农场集合,如何代价最小...

山中竹笋 270

PID调参避坑指南:为什么你的电机总是震荡?从比例带积分到微分实战解析

本文针对电机控制中常见的震荡问题,提供了一份PID调参实战指南。文章深入解析了比例(P)、积分(I)、微分(D)三个参数对系统稳定性的影响,揭示了比例增益过大是导致震荡的关键原因之一,并给出了从纯比例调试到引入积分、微分的系统化调参流程与避坑要点,帮助工程师快速实现电机平稳、精准的控制。

weixin_28223453的博客 457

usaco Chapter 3 section 3.2 Sweet Butter

/*                                  Sweet ButterFarmer 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 know

china1000的专栏 591

Oracle EBS Interface/API(42) -AP发票客制化付款申请

背景 在OA系统上,选择Oracle EBS的某张AP发票做付款申请,需要类似于锁定该发票以便付款,其他用户或者下次用户不能再次选择该发票做付款申请。 快速参考 参考点 内容 功能导航 N: AP->Invoices->Entry>Invoices 并发请求 None 基表 AP.AP_INV_SELECTION_CRITERIA_ALLAP.AP_SELECTED_INVOICES_ALL API 参考下面介绍 错误信息表 None 接口Form

陈显平的专栏-Oracle EBS 1200

在 ABAP Cloud 里优雅地调用 HTTP 服务:新一代 HTTP Client 全面实践与落地指南

摘要: 随着ABAP Cloud时代的到来,HTTP通信在SAP系统集成中的角色日益重要。传统CL_HTTP_CLIENT存在维护成本高、边界模糊等问题,而新一代ABAP HTTP Client采用Destination驱动的调用方式,通过CL_HTTP_DESTINATION_PROVIDER支持四种创建方式(URL、通信安排、BTP Destination服务、SM59连接)。该模型将连接配置与业务逻辑分离,使URL、认证等参数可集中管理。典型调用流程包括:基于Destination创建Client,设

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 251

USACO SECTION 3.2 Sweet Butter

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 <=

337

如何在Windows上安装Node-RED?

Node-RED 是一款基于流程的可视化编程工具,专注于物联网(IoT)和自动化应用的快速开发,其核心特点是通过拖拽节点构建数据流网络,实现低代码/无代码编程。

猿小白的博客 1171

USACO Section 3.2 Sweet Butter (butter)

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 &l...

weixin_30256901的博客 128

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1923

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1486

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1405

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 989

USACO section 3.1 Stamps(dp)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

nealgavin的专栏 980

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 937

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 913
上一篇: USACO section 3.2 Magic Squares(STL+广搜)
下一篇: hdu 2527 Safe Or Unsafe(哈夫曼编码)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值