Description

On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera with a few ships, starting a serious of voyages of finding a new route to India. As you know, just in those voyages, Columbus discovered the America continent which he thought was India. 
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

There are several test cases.  The first line in the input is an integer T indicating the number of test cases ( 0 < T <= 10).  For each test case:  The first line contains an integer N, meaning there are N kinds of goods ( 0 < N <= 20). These N kinds are numbered from 1 to N. 
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

For each test case:  Please output N lines at first. Each line contains two integers n and p, meaning that the “actual price” of the goods of kind n is p gold coins. These N lines should be in the ascending order of kind No. . 
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)的更多相关文章

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

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

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

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

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

  6. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

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

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

  9. HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]

    标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...

随机推荐

  1. OOP导论系列---抽象过程

    OOP导论系列---抽象过程 所有编程语言都提供抽象机制.可以认为,人们所能解决的问题的复杂性直接取决于抽象的类型和质量.所谓“类型”是指“所抽象的是什么?”你可以抽取待求解问题的任何概念化构件,如: ...

  2. 集群、RAC和MAA

    集群:是一种由两台或多台节点机构成的松散耦合的计算节点集合,这个集合在整个网络中表现为单一的系统,并通过单一接口进行使用和管理.给用户提供网络服务或应用程序的单一视图.大多数模式下,集群中所有计算机都 ...

  3. PHP留言板的实现思路

    本文实例为大家分享了php留言板的实现思路,供大家参考,具体内容如下:1.创建一个存放留言信息的文件名2.获取表单中的数据给一个变量3.判断文件的时候存在4.对文件执行写的操作,在这之前,注意打开文件 ...

  4. thinkphp 5.1/tp5.1 route路由bug

    tp5.1下面RuleItem类中,match方法. 如果同一个控制器下面,写了两个路由,后一个路由比包含前一个路由,则访问后一个路由地址的时候,会跳转到前面定义的那个路由

  5. PHP读取zip包

    $filename = $this->upload->data('file_name');   //得到文件夹(此处是CI框架上传文件之后得到文件名称) $file_root = 'can ...

  6. 『Linux基础 - 2 』操作系统,Linux背景知识和Ubuntu操作系统安装

    这篇笔记记录了以下几个知识点: 1.目前常见的操作系统及分类,虚拟机 2.Linux操作系统背景知识,Windows和Linux两个操作系统的对比 3.在虚拟机中安装Ubuntu系统的详细步骤 OS( ...

  7. 北京Uber优步司机奖励政策(1月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”

    报错代码: sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) 解决 ...

  9. DSP5509之采样定理

    1. 在实际种信号是模拟连续的,但是AD采样确实离散的数字的,根据采样定理,采样频率要是模拟信号的频率2倍以上采样到的值才没问题. 2. 打开工程 unsigned ]; main() { int i ...

  10. 利尔达NB-IOT模组Coap数据AT+NMGS发送时返回-513的原因

    1. 利尔达NB-IOT模组使用AT+NMGS发送数据,返回-513的问题,大致有3种可能性,在硬件上,模组的射频电路分为A型和B型模组,所以烧写固件的时候,也要分为A和B型固件,如果烧写反了,那么R ...