HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
Description
Because the ships are not large enough and there are seldom harbors in his route, Columbus had to buy food and other necessary things from savages. Gold coins were the most popular currency in the world at that time and savages also accept them. Columbus wanted to buy N kinds of goods from savages, and each kind of goods has a price in gold coins. Columbus brought enough glass beads with him, because he knew that for savages, a glass bead is as valuable as a gold coin. Columbus could buy an item he need only in four ways below:
1. Pay the price all by gold coins. 2. Pay by ONE glass bead and some gold coins. In this way, if an item’s price is k gold coins, Columbus could just pay k � 1 gold coins and one glass bead. 3. Pay by an item which has the same price. 4. Pay by a cheaper item and some gold coins.
Columbus found out an interesting thing in the trade rule of savages: For some kinds of goods, when the buyer wanted to buy an item by paying a cheaper item and some gold coins, he didn’t have to pay the price difference, he can pay less. If one could buy an item of kind A by paying a cheaper item of kind B plus some gold coins less than the price difference between B and A, Columbus called that there was a “bargain” between kind B and kind A. To get an item, Columbus didn’t have to spend gold coins as many as its price because he could use glass beads or took full advantages of “bargains”. So Columbus wanted to know, for any kind of goods, at least how many gold coins he had to spend in order to get one � Columbus called it “actual price” of that kind of goods.
Just for curiosity, Columbus also wanted to know, how many kinds of goods are there whose “actual price” was equal to the sum of “actual price” of other two kinds.
Input
Then N lines follow, each contains two integers Q and P, meaning that the price of the goods of kind Q is P. ( 0 <Q <=N, 0 < P <= 30 ) The next line is a integer M( 0 < M <= 20 ), meaning there are M “bargains”.
Then M lines follow, each contains three integers N1, N2 and R, meaning that you can get an item of kind N2 by paying an item of kind N1 plus R gold coins. It’s guaranteed that the goods of kind N1 is cheaper than the goods of kind N2 and R is none negative and less than the price difference between the goods of kind N2 and kind N1. Please note that R could be zero.
Output
Then output a line containing an integer m, indicating that there are m kinds of goods whose “actual price” is equal to the sum of “actual price” of other two kinds.
题目大意:要买n种东东,每种东东有一个价格。可以用4种方式买这些东东:1、直接用金币全额购买(没用的条件……);2、若东东k元,付k-1个金币加一个珠子买。;3、用一个东东加一些钱交换另一个东东。4:等价值的东东可以交换。问每个东东最少用多少钱可以卖到,有几个东东满足其实付等于另外两个东东的实付之和。
思路:图论水题。附加源点S,从S到每一个东东连边,代价为k-1(用珠子不用是浪费噢);N1能外加x元换到N2,就从N1连边到N2,代价为x;价格相同的东东之间连双向边,代价为0。每个东东到源点的最短路径就是最少用的钱,就是实际代价。最后要算的m,这么小的范围,果断暴力$O(n^3)$枚举啦管他呢。
PS:无聊测试了一下输入前面的序号确实是按顺序给的(其实不给也没什么关系吧……)
代码(0MS):
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = * MAXN * MAXN;
const int INF = 0x3fff3fff; struct Node {
int id, val;
void read() {
scanf("%d%d", &id, &val);
}
bool operator < (const Node &rhs) const {
return id < rhs.id;
}
}; Node a[MAXN];
int dis[MAXN], head[MAXN], vis[MAXN];
int next[MAXE], to[MAXE], cost[MAXE];
int ecnt, n, m; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
} void Dijkstra(int st, int n) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, , sizeof(vis));
dis[st] = ;
for(int x = ; x < n; ++x) {
int u, minDis = INF;
for(int i = ; i < n; ++i)
if(!vis[i] && dis[i] < minDis) u = i, minDis = dis[i];
vis[u] = true;
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(!vis[v] && dis[v] > dis[u] + cost[p]) dis[v] = dis[u] + cost[p];
}
}
} void solve() {
init();
scanf("%d", &m);
while(m--) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
int st = ;
for(int i = ; i <= n; ++i) add_edge(st, i, a[i].val - );
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) {
if(i == j || a[i].val != a[j].val) continue;
add_edge(i, j, );
add_edge(j, i, );
}
}
Dijkstra(st, n + );
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = ; i <= n; ++i) a[i].read();
sort(a + , a + n + );
solve();
for(int i = ; i <= n; ++i) printf("%d %d\n", i, dis[i]);
int ans = ;
memset(vis, , sizeof(vis));
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) {
if(i == j) continue;
for(int k = ; k <= n; ++k) {
if(k == i || k == j) continue;
if(vis[k] || dis[i] + dis[j] != dis[k]) continue;
vis[k] = true;
++ans;
}
}
}
printf("%d\n", ans);
}
}
HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)的更多相关文章
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...
- HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)
Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...
- HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- UVA 11883 Repairing a Road(最短路径+暴力枚举)
You live in a small town with R bidirectional roads connecting C crossings and you want to go from c ...
- HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)
Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...
- HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]
标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...
随机推荐
- 四种常见的 POST 提交数据方式对应的content-type
原文地址:https://www.cnblogs.com/wushifeng/p/6707248.html application/x-www-form-urlencoded 这应该是最常见的 POS ...
- My collage goals
PART ONE: THE GOALS OF GRADE ONE 1, Try my best to improve my GPA , keep it around 4.0 2, Learn mor ...
- 正则表达式-Regular expression学习笔记
正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...
- 前端的字符串时间如何自动转换为后端Java的Date属性,介绍springMVC中如何解决时间转换问题
平常在开发过程中,前端选择时间一般都要使用时间选择插件,但是这种插件选出来的时间都是字符串类型,我们该怎么转换为后端的Date呢?/? 前端效果如下(笔者用的是layDate5.0插件): 修改前的后 ...
- webpack-dev-server 多入口自动刷新,支持对象
万物的来源~webpack 本身 watch webpack watch 传送门 webpack 可以监听文件变化,当它们修改后会重新编译 watch boolean 启用 Watch 模式.这意味着 ...
- Java三种代理模式
本文转自:https://mp.weixin.qq.com/s/nBmbNP2mR7ei-lDsuOxjWg 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象 ...
- Flume:source和sink
Flume – 初识flume.source和sink 目录基本概念常用源 Source常用sink 基本概念 什么叫flume? 分布式,可靠的大量日志收集.聚合和移动工具. events ...
- Python学习手册之__main__ 模块,常用第三方模块和打包发布
在上一篇文章中,我们介绍了 Python 的 元组拆包.三元运算符和对 Python 的 else 语句进行了深入讲解,现在我们介绍 Python 的 __main__ 模块.常用第三方模块和打包发布 ...
- sas的使用
1.建表 /*************************************/ /* create the second input data set */ /*************** ...
- BZOJ1433_假期的宿舍_KEY
题目传送门 二分图匹配的题目. 但建边有一定难度,关系比较复杂. 首先要统计总共需要几张床. 在校且住校的会需要一张床,不住校的需要一张床. 然后对于在校且住校的与自己的床连边,不住校的与认识的住校的 ...