2038 香甜的黄油

USACO

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 查看运行结果
 
 
题目描述 Description

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。

农夫John很狡猾。他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。

农夫John知道每只奶牛都在各自喜欢的牧场呆着(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

输入描述 Input Description

第一行: 三个数:奶牛数N,牧场数P(2<=P<=800),牧场间道路数C(1<=C<=1450).

第二行到第N+1行: 1到N头奶牛所在的牧场号.

第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距(1<=D<=255),当然,连接是双向的.

输出描述 Output Description

一行 输出奶牛必须行走的最小的距离和.

样例输入 Sample Input
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
样例图形
         P2
P1 @--1--@ C1
\ |\
\ | \
5 7 3
\ | \
\| \ C3
C2 @--5--@
P3 P4
样例输出 Sample Output
8
{说明: 放在4号牧场最优. }
数据范围及提示 Data Size & Hint

见描述

思路:

  spfa;

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define INF 0x7ffffff using namespace std; struct EdgeType {
int to,dis,next;
};
struct EdgeType edge[]; int n,p,m,bel[],cnt,ans=INF;
int head[],dis[],que[]; bool if_[]; char Cget; inline void in(int &now)
{
now=,Cget=getchar();
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} int main()
{
in(n),in(p),in(m);int u,v,w;
for(int i=;i<=n;i++) in(bel[i]);
for(int i=;i<=m;i++)
{
in(u),in(v),in(w);
edge[++cnt].to=v,edge[cnt].next=head[u],edge[cnt].dis=w,head[u]=cnt;
edge[++cnt].to=u,edge[cnt].next=head[v],edge[cnt].dis=w,head[v]=cnt;
}
for(int s=;s<=p;s++)
{
for(int i=;i<=p;i++) dis[i]=INF;
int he=,ta=;if_[s]=true,dis[s]=;
que[ta++]=s;
while(he<ta)
{
int pos=que[he];he++;
for(int i=head[pos];i;i=edge[i].next)
{
if(dis[edge[i].to]>dis[pos]+edge[i].dis)
{
dis[edge[i].to]=dis[pos]+edge[i].dis;
if(!if_[edge[i].to])
{
if_[edge[i].to]=true;
que[ta++]=edge[i].to;
}
}
}
if_[pos]=false;
}
int tot=;
for(int i=;i<=n;i++) tot+=dis[bel[i]];
if(tot<ans) ans=tot;
}
cout<<ans;
return ;
}

AC日记——香甜的黄油 codevs 2038的更多相关文章

  1. T2038 香甜的黄油 codevs

    http://codevs.cn/problem/2038/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond   题目描述 Description 农夫John ...

  2. AC日记——接龙游戏 codevs 1051

    1051 接龙游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 给出了N个单词,已经按长度排好了序.如果 ...

  3. AC日记——地鼠游戏 codevs 1052

    1052 地鼠游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...

  4. AC日记——鬼谷子的钱袋 codevs 2998

    2998 鬼谷子的钱袋 2006年省队选拔赛湖南  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master    题目描述 Description 鬼谷子非常聪明,正 ...

  5. AC日记——舒适的路线 codevs 1001 (并查集+乱搞)

    1001 舒适的路线 2006年  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description Z小镇是 ...

  6. AC日记——逃跑的拉尔夫 codevs 1026 (搜索)

    1026 逃跑的拉尔夫  时间限制: 1 s    空间限制: 128000 KB    题目等级 : 黄金 Gold 题解       题目描述 Description   年轻的拉尔夫开玩笑地从一 ...

  7. AC日记——自然数和分解 codevs 2549

    自然数和分解 思路: 水题: 代码: #include <bits/stdc++.h> using namespace std; ][]; int main() { cin>> ...

  8. AC日记——郁闷的出纳员 codevs 1286

    郁闷的出纳员 思路: 设工资下限为ko,然后ko--(因为要小于工资下限): 设cur为记录工资增长,降低: 设第i个人的工资为pos: 对应的四种操作: 插入:cur-pos-ko: 增长:cur- ...

  9. AC日记——营业额统计 1296 codevs

    1296 营业额统计 2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description Tiger ...

随机推荐

  1. monkeyrunner之控件ID不存在或重复(转载lynnLi)

    我们在用monkeyrunner进行Android自动化时,通过获取坐标点或控件ID进行一系列操作.由于使用坐标点时,屏幕分辨率一旦更改,则代码中用到坐标的地方都要修改,这样导致代码的复用率较低.因此 ...

  2. Mac 安装和卸载 Mysql5.7.11 的方法

    安装 去http://www.mysql.com/downloads/, 选择最下方的MySQL Community Edition,点击MySQL Community Server的download ...

  3. MFC:AfxLoadLibrary-将指定的 DLL 映射到调用进程的地址空间

    Visual Studio 2012 - Visual C++ LoadLibrary 和 AfxLoadLibrary 进程调用 LoadLibrary (或 AfxLoadLibrary) 以显式 ...

  4. Luogu P1782 旅行商的背包

    题目传送门 卡常背包果然名不虚传 算法主体就是两种背包分开跑,先跑多重背包,再跑奇货 不知道为什么,这题二进制拆分好像要比单调队列优化快一些 然后这题毒瘤的地方就出来了: 如果一件物品的体积\(\ti ...

  5. please upgrade your plan to create a new private reposiory

    请升级你的计划来创建一个新的私人仓库 提交仓库到github,要公开,除非买他们服务,所以把勾去掉就好了keep this code private

  6. grep-sed命令用法:

    用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND   echo -n   ...

  7. pytorch导入错误so: undefined symbol: _Z11libshm_initPKc

    首先删除torch文件 或者直接卸载 删除会更彻底 https://blog.csdn.net/qq_37674858/article/details/88870124 但是会发现卸载重装pytorc ...

  8. (转)Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等

    本文转自http://blog.csdn.net/totogo2010/article/details/7714960 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 - ...

  9. 关于get_magic_quotes_gpc()函数

    function sqlReplace($str) { $strResult = $str; if(!get_magic_quotes_gpc()) //如果 gpc 没有开的话 { $strResu ...

  10. LeetCode 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...