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. MFC学习知识点20160715

    1.   sizeof()  :返回所查询目标所占用字节数 _countof() :返回所查询目标所含有元素个数 _countof 是 C++中计算一个固定大小数组长度的宏,比如: T arr[10] ...

  2. Java NIO 缓冲区学习笔记

    Buffer其实就是是一个容器对象,它包含一些要写入或者刚读出的数据.在NIO中加入Buffer对象,体现了新库与原I/O的一个重要区别.在面向流的I/O中,您将数据直接写入或者将数据直接读到Stre ...

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

    每天上班闲着没事干好蛋疼啊.周报都不知道怎么写了. 今天加上了毕梦琪,哎,不知咋聊.好烦. 要搞angelarjs了,希望有机会,多搞点东西,要当架构师,哈哈. 量子计算,什么高深的东西,百度百科了下 ...

  4. 在Dashboard中显示课表/日程表

    对于使用Mac系统的朋友们来说,Dashboard一定并不陌生.通过Dashboard我们可以方便地添加小组件,查看日历,天气,便签等等.然而,这些都是“定制”的内容.如何在Dashboard中显示自 ...

  5. ES6学习(1)——如何通过babel将ES6转化成ES5

    使用babel编译ES6 babel是一个工具,可以通过多个平台,让js文件从ES6转化成ES5,从而支持一些浏览器并未支持的语法. Insall babel $ sudo npm install b ...

  6. C++中#include包含头文件带 .h 和不带 .h 的区别

    C++中#include包含头文件带 .h 和不带 .h 的区别? 如 #include <iostream> 和 #include <iostream.h> 包含的东西有哪些 ...

  7. 【Java】Treeset实现自定义排序

    两个类,一个学生类,含姓名和出生日期两个属性:还有一个学生排序类,重写compare函数,自定义排序规则是先比较出生日期,如果相同再比较姓名字母 package birthday; import ja ...

  8. 三、FreeMarker 模版开发指南 第三章 模版

    章节内容如下:   总体结构 指令 表达式 插值 一.总体结构 实际上你用程序语言编写的程序就是模板,模板也被称为FTL(代表FreeMarker模板语言).这是为编写模板设计的非常简单的编程语言. ...

  9. OpenNebula config配置详情

    AUTH_MAD=AUTHN=ssh,x509,ldap,server_cipher,server_x509,EXECUTABLE=one_auth_mad DATASTORE_BASE_PATH=/ ...

  10. JdbcTemplate增删改查

    1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...