Columbus’s bargain

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1721    Accepted Submission(s):
431

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

 
Sample Input
1
4
1 4
2 9
3 5
4 13
2
1 2 3
3 4 6
 
 
Sample Output
1 3
2 6
3 4
4 10
1
 
 
Source
 
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int ms=;
const int inf=0xffffff;
struct edge
{
int u,v,w,next;
}edges[ms*ms];
int head[ms],price[ms],dis[ms],cnt,n,m;
bool vis[ms];
void add_edge(int u,int v,int w)
{
edges[cnt].u=u;
edges[cnt].v=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
return ;
} void input()
{
int i,j,id,pri;
memset(vis,false,sizeof(vis));
memset(head,-,sizeof(head));
fill(dis,dis+ms,inf);
cnt=;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d %d",&id,&pri);
price[id]=pri;
add_edge(,id,pri-);
}
for(i=;i<=n;i++)
for(j=i+;j<=n;j++)
if(price[i]==price[j])
{ add_edge(i,j,);
add_edge(j,i,);
}
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d",&i,&j,&pri);
add_edge(i,j,pri);
}
return ;
}
void spfa()
{
int i,s=,to;
queue<int> que;
dis[s]=;
que.push(s);
vis[s]=true;
while(!que.empty())
{
// s=que.top(); 栈
s=que.front();
que.pop();
for(i=head[s];i!=-;i=edges[i].next)
{
to=edges[i].v;
if(dis[to]>dis[s]+edges[i].w)
{
dis[to]=dis[s]+edges[i].w;
if(!vis[to])
{
vis[to]=true;
que.push(to);
}
}
}
vis[s]=false;
}
return ;
}
void solve()
{
int i,j,k,ans=;
spfa();
for(i=;i<=n;i++)
printf("%d %d\n",i,dis[i]);
bool flag;
for(i=;i<=n;i++)
for(flag=true,j=;j<=n&&flag;j++)
for(k=j+;k<=n&&flag;k++)
if(i!=j&&i!=k)
{
if(dis[i]==dis[j]+dis[k])
{
ans++;
flag=false;
}
}
printf("%d\n",ans);
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
input();
solve();
}
return ;
}

Columbus’s bargain的更多相关文章

  1. POJ 3835 &amp; HDU 3268 Columbus’s bargain(最短路 Spfa)

    题目链接: POJ:http://poj.org/problem?id=3835 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268 Problem ...

  2. hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

  3. HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

  4. <老友记>学习笔记

    这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...

  5. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  6. 读书笔记--SQL必知必会09--汇总数据

    9.1 聚集函数 聚集函数(aggregate function),对某些行运行的函数,计算并返回一个值. 使用聚集函数可以汇总数据而不必将涉及的数据实际检索出来. 可利用标准的算术操作符,实现更高级 ...

  7. 读书笔记--SQL必知必会11--使用子查询

    11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...

  8. 读书笔记--SQL必知必会15--插入数据

    15.1 数据插入 使用INSERT语句将行插入(或添加)到数据库表.可能需要特定的安全权限. 插入完整的行 插入行的一部分 插入某些查询的结果 15.1.1 插入完整的行 要求指定表名和插入到新行中 ...

  9. 读书笔记--SQL必知必会16--更新和删除数据

    16.1 更新数据 使用UPDATE语句更新或修改表中的数据.必须有足够的安全权限. 更新表中的特定行 更新表中的所有行 使用UPDATE时一定要细心,不要省略WHERE子句. SET命令用来将新值赋 ...

随机推荐

  1. 《锋利的Jquery第二版》读书笔记 第一章

    按照书本介绍顺序整理jquery库相关的语法.要点. window.onload与$(document).ready()功能类似,前者需要所有资源加载完毕,且不能同时编写多个:后者加载完DOM结构即执 ...

  2. redis.conf的配置

    daemonize yes   :  redis server 实例是否以后台方式运行 , no:不以后台方式运行(默认) , yes:以后台方式运行. requirepass 密码 :  密码最好长 ...

  3. java 图像分析与处理库

    OpenCv4Android: http://opencv.org/platforms/android.html opencv官方中文文档: http://www.opencv.org.cn/open ...

  4. 第二百五十二天 how can I 坚持

    明天就要去旅游了...还不知道去哪呢,只知道要滑雪,要泡温泉,还要去西柏坡..哈哈. 其他没什么了吧.只是昨晚刷的鞋还没干,不知道明天会不会干,明天还得早走会,九点之前就得到. 还不知道坐车坐多长时间 ...

  5. JQuery学习使用笔记 -- JQuery插件开发

    内容转载自 http://www.css88.com/archives/4821 扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间.这篇文章将概述jQuery插件开发的基本知识,最 ...

  6. [转]Oracle 操作字符串的函数

    转至:http://yedward.net/?id=62 (1)oracle中实现截取字符串:substr substr(string, start_position, [length]) 其中,st ...

  7. swfupload用法总结

    <script src="${base}/thirdparty/swfupload/swfupload.js" type="text/javascript" ...

  8. C#学习笔记(二):继承、接口和抽象类

    继承 密封类 密封类(关键字sealed)是不允许其它类继承的,类似Java中的final关键字. public sealed class SealedClassName { //... } 初始化顺 ...

  9. CloudStack 使用生态统计

    http://shapeblue.com/

  10. Android Layout_Gravity和Gravity

    简单来说layout_gravity表示子控件在父容器的位置,gravity表示控件内容在控件内的位置. 上面图片的xml代码 <?xml version="1.0" enc ...