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. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...

  2. 在Ubuntu6.06 在搭建SVN服务器及在windows建立svn+ssh客户端

    部门现在使用的Linux系统是Ubuntu6.06,内核版本为2.6.15-57-386.由于系统比较老,所有用网上介绍的方法搭建SVN服务器经常出错,所以参考文章[1],将自己的搭建过程记录下. 1 ...

  3. C++多态分析(polymorphisn)

    前言 借着这次学校的生产实习来回顾下C++的多态,这里讨论下C++的多态以及实现原理.我都是在QT下使用C++的,就直接在QT下进行演示了 多态介绍 面向对象语言中最核心的几个理念就是:封装.继承.多 ...

  4. Hadoop 删除节点步骤

    1.在hadoop1.1.1/conf 下新建文件 nn-excluded-list 并写入要删除的节点名称或者IP 一个节点 一行 如: mos5200app cmpaknwom rac7 2.分发 ...

  5. Python覆盖率分析工具_Coverage

    easy_install安装: easy_install coverage 运行: coverage run test.py coverage report

  6. HTML5每日一练之details展开收缩标签的应用

    details标签的出现,为我们带来了更好的用户体验,不必为这种收缩展开的效果再编写JS来实现.注:目前仅Chrome支持此标签. details有一个新增加的子标签——summary,当鼠标点击su ...

  7. html5 标签

    按字母顺序排列的标签列表 4: 指示在 HTML 4.01 中定义了该元素 5: 指示在 HTML 5 中定义了该元素 标签 描述 4 5 <!--...--> 定义注释. 4 5 < ...

  8. JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

    获取数据库自动生成的主键 [孤立的技术是没有价值的],我们这里只是为了了解具体的实现步骤:我们在插入数据的时候,经常会需要获取我们插入的这一行数据对应的主键值. 具体的代码实现: /** * 获取数据 ...

  9. TQMsgPack序列还原clientdataset.data

    序列 procedure TForm1.Button2Click(Sender: TObject);var msgpack: TQMsgPack;begin msgpack := TQMsgPack. ...

  10. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...