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);
}

 

【WRF-Urban】WUDAPT详解:An Urban Weather, Climate, and Environmental Modeling Infrastructure WUDAPT详解:An Urban Weather, Climate, and Environmental Modeling Infrastructure 阅读详情

相关推荐

Power BI应用案例:淘宝用户行为分析实战

一、案例背景 基于千万级的淘宝用户的使用记录,对淘宝用户的行为进行分析,具体指标包括:日PV、日UV、小时PV、小时UV、不同用户行为的PV、用户购买次数分布、用户购买频次分布、活跃用户消费情况等。通过本案例,可以基本看看Power BI都能分析达到怎么样的效果。 二、数据来源及结构 阿里天池官方发布的1万用户在2014年双十二期间的行为记录,数据量达1200万+。 数据下载地址:...

灰哥数据智能的专栏 1万+

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

以太网以及车载以太网概述

千兆网线和百兆网线区别 1、所谓的百兆网线其实就是 五类线 和超五类线 ,而千兆网线主要是指六类线 和超六类线 ,另外还有古老的十兆网线也就是指 四类线 和 三类线 等。 2、在使用上,其实千兆网线和百兆网线是没什么区别的,千兆网线完全兼容百兆网络。 3、百兆网线的线芯是8芯,但上传和下载只用到了其中的4根,而千兆网线需要完全用到8根。 4、另外在线芯的排布上,千兆网线的线芯中间有个十字形的塑料架,用来把四组8芯的线分开,这个百兆网线是没有的, 5、所有的网线在外皮上都有种类标识,CAT5 CAT

$好记性还是要多记录$ 4074

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

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

zzyzzy12 1126

光模块MCU开发全解析:从架构原理到固件调试实战

在高速光模块中,MCU并非传统印象里的核心处理器,而是负责数字诊断、闭环控制和协议管理的嵌入式管家。随着800G、1.6T模块的普及,MCU与DSP/SoC的协同、I2C总线通信、ADC采集、APC/TEC控制等低速域任务愈发关键。理解MCU的选型规格、启动流程和固件设计,是保障光模块稳定运行的基础。本文从光模块架构切入,剖析MCU的角色定位,详细拆解I2C从机实现、电芯片配置、校准与升级等工程细节,并结合实际调试经验梳理常见故障与排查思路,为嵌入式工程师提供一份可落地的光模块MCU开发指南。

weixin_29192365的博客 364

openEuler在虚拟机的网络配置

ethernet0.virtualDev 可以配置成其他类型的虚拟网络适配器。常见的选项包括:vlance - 用于模拟 AMD PCnet32 LANCE 网络适配器,支持10Mbps。e1000 - 用于模拟 Intel E1000 千兆网络适配器,支持1Gbps。e1000e - 用于模拟更现代的 Intel E1000e 千兆网络适配器。vmxnet - 用于 VMware 的自定义网络适配器,性能更好但需要安装 VMware Tools。

FHY1221的博客 2932

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

如何利用GMS构建地下水数值模拟模型?

第一步:点图中三维网格的图标然后点Grid→Create Grid这个界面是用于创建一个有限差分网格的设置窗口。它允许你定义网格在三维空间中的范围、分辨率以及其他相关属性。这个界面提供了一个直观的方式来定义一个三维的有限差分网格,你需要指定每个维度上的范围、划分的单元格数量,以及一些控制单元格大小分布和网格方向的参数。这个网格通常会被用于数值模拟,例如地下水流模拟。在点击 "OK" 之前,请务必仔细检查所有参数是否符合你的建模需求。1. X-Dimension (X 轴维度)2. Y-Dimension (

weixin_44259522的博客 2093

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

概率论与数理统计基础概念与重要定义汇总

文章目录一、随机事件和概率1:互斥,对立,独立事件的定义和性质。2:概率,条件概率和五大概率公式3:古典型,几何型概率和伯努利试验 一、随机事件和概率 1:互斥,对立,独立事件的定义和性质。 互斥事件\color{red}\textbf{互斥事件}互斥事件 事件A和B的交集为空,A与B就是互斥事件,也叫互不相容事件。也可叙述为:不可能同时发生的事件。如A∩B为不可能事件(A∩B=Φ),那么称事件A与事件B互斥,其含义是:事件A与事件B在任何一次试验中不会同时发生。 则P(A+B)=P(A)+P(B)(这个公

csyifanZhang的博客 1万+

LNA设计学习心得记录----MOS管的选取

LNA设计学习心得---MOS管的选取仿真

weixin_40359668的博客 6127

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

第9集:AI 驱动的智能日志分析!用 RAG 实现秒级日志检索与异常检测

文章摘要:AI驱动的智能日志分析系统 本文介绍了一个基于RAG(检索增强生成)技术的智能日志分析解决方案,旨在解决传统日志系统中的三大痛点:机械的关键词搜索、缺乏语义理解、告警与日志割裂。系统通过以下步骤实现: 环境准备:安装必要的Python依赖库,包括日志处理、Elasticsearch/Loki连接和时序异常检测工具。 核心设计:构建日志处理流水线,分为日志采集、向量化存储和AI检索分析三个阶段,创建专门的日志分析Agent。 技术实现:使用ChromaDB存储日志向量,通过HuggingFace E

qq_2937655951的博客 80

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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、付费专栏及课程。

余额充值