BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020
题意:
FJ开车去买K份食物。
如果他的车上有X份食物,每走一里就花费X元。
FJ的城市是一条线,总共n里路,有n+1个地方,标号0~n。
FJ从0开始走,到n结束(不能往回走),要买m份食物。
城里有t个商店,每个商店的位置是x[i](一个点上可能有多个商店),有f[i]份食物,每份c[i]元。
问到达n并买m份食物的最小花费。
题解:
贪心。
每一份食物实际的话费 = 它的价格 + 剩下的路程
所以按照实际花费排序,然后依次选够m个就好啦。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define MAX_N 100005
#define INF 10000000 using namespace std; struct Edge
{
int dest;
int len;
Edge(int _dest,int _len)
{
dest=_dest;
len=_len;
}
Edge(){}
}; struct Node
{
int idx;
int dis;
Node(int _idx,int _dis)
{
idx=_idx;
dis=_dis;
}
Node(){}
friend bool operator > (const Node &a,const Node &b)
{
return a.dis>b.dis;
}
}; int n,m;
int s,t1,t2;
int ans=INF;
int dis[MAX_N];
vector<Edge> edge[MAX_N];
priority_queue<Node,vector<Node>,greater<Node> > q; void djikstra(int start)
{
memset(dis,-,sizeof(dis));
dis[start]=;
q.push(Node(start,));
while(!q.empty())
{
Node now=q.top();
q.pop();
if(dis[now.idx]<now.dis) continue;
for(int i=;i<edge[now.idx].size();i++)
{
Edge temp=edge[now.idx][i];
if(dis[temp.dest]==- || dis[temp.dest]>dis[now.idx]+temp.len)
{
dis[temp.dest]=dis[now.idx]+temp.len;
q.push(Node(temp.dest,dis[temp.dest]));
}
}
}
} void read()
{
cin>>m>>n>>s>>t1>>t2;
int a,b,v;
for(int i=;i<m;i++)
{
cin>>a>>b>>v;
edge[a].push_back(Edge(b,v));
edge[b].push_back(Edge(a,v));
}
} void solve()
{
djikstra(t1);
ans=min(ans,dis[s]+dis[t2]);
djikstra(t2);
ans=min(ans,dis[s]+dis[t1]);
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】的更多相关文章
- 2020: [Usaco2010 Jan]Buying Feed, II
2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 220 Solved: 162[ ...
- 【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2020 和背包差不多 同样滚动数组 f[j]表示当前位置j份食物的最小价值 f[j]=min(f[j- ...
- BZOJ2020: [Usaco2010 Jan]Buying Feed II
[传送门:BZOJ2020] 简要题意: 约翰开车回家,遇到了双十一节,那么就顺路买点饲料吧.回家的路程一共有E 公里,这一路上会经过N 家商店,第i 家店里有Fi 吨饲料,售价为每吨Ci 元.约翰打 ...
- bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】
不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- USACO Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...
- 【P2616】 【USACO10JAN】购买饲料II Buying Feed, II
P2616 [USACO10JAN]购买饲料II Buying Feed, II 题目描述 Farmer John needs to travel to town to pick up K (1 &l ...
- bzoj 1783: [Usaco2010 Jan]Taking Turns
1783: [Usaco2010 Jan]Taking Turns Description Farmer John has invented a new way of feeding his cows ...
- BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...
随机推荐
- TPM:dTPM(硬件)和fTPM(固件模拟的软件模块)
转:Bitlocker.TPM和系统安全 自从微软在Windows Vista首次引入Bitlocker以来,它已经越来越多的出现在我们的周围.尤其是企业用户,Bitlocker的保护已经变得不可缺少 ...
- Spring中的scope配置和@scope注解
Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...
- 系统安全-Google authenticator
对于某些人来说,盗取密码会比你想象的更简单 以下任意一种常见的操作都可能让你的密码面临被盗的风险: 在多个网站上使用同一个密码 从互联网上下载软件 点击电子邮件中的链接 两步验证可以将别有用心的人阻 ...
- java 中 wait和notify的用法
package com.test; public class OutputThread { public static Object lockObj=new Object(); public stat ...
- js 中的 prototype 和 constructor
var a=function(){ this.msg="aa"; } a.prototype.say=function(){ alert('this is say');} 1.只有 ...
- MagicalRecord使用教程【转载】
原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...
- 牛牛有一个鱼缸。鱼缸里面已经有n条鱼,每条鱼的大小为fishSize[i] (1 ≤ i ≤ n,均为正整数),牛牛现在想把新捕捉的鱼放入鱼缸。鱼缸内存在着大鱼吃小鱼的定律。经过观察,牛牛发现一条鱼A的大小为另外一条鱼B大小的2倍到10倍(包括2倍大小和10倍大小),鱼A会吃掉鱼B。考虑到这个,牛牛要放入的鱼就需要保证:1、放进去的鱼是安全的,不会被其他鱼吃掉 2、这条鱼放进去也不能吃掉其他鱼
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include<vector> #include<algorithm> #inc ...
- Two stage U-Boot design
In AM335x the ROM code serves as the bootstrap loader, sometimes referred to as the Initial Program ...
- Centos内核版本升级
- Java多线程面试问题
这篇文章主要是对多线程的面试问题进行总结的,罗列了40个多线程的问题. 1. 多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓 ...