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. 线性模型(1):Perceptron Learning Algorithm (PLA)

    此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> (一) PLA算法是基本的binary Classification算法. 一个基本的问题是,对于银行,假设我知道 ...

  2. Windows 8.1及Windows8 JDK环境变量配置

    一.首先安装JDK JDK:http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html 根据操作系统选择相应的版本 二. ...

  3. JAVA与网络开发(TCP:Socket、ServerSocket;UDP:DatagramSocket、DatagramPacket;多线程的C/S通讯、RMI开发概述)

    通过TCP建立可靠通讯信道 1)为了对应TCP协议里的客户端和服务器端,Socket包提供了Socket类和ServerSocket类. 2)Socket类构造函数及相关方法 Public Socke ...

  4. javascript判断NaN

    功能: isNaN() 函数用于检查其参数是否是非数字值. 语法: isNaN(x) x 必需.要检测的值. 返回值: 如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 tr ...

  5. HTTP常见错误 400/401/403/404/500及更多

    HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...

  6. mmap和普通文件读写的区别和比较 & mmap的注意点

    参考 http://www.cnblogs.com/huxiao-tee/p/4660352.html 对linux文件系统不了解的朋友,请参阅我之前写的博文<从内核文件系统看文件读写过程> ...

  7. Elasticsearch和mysql数据同步(elasticsearch-jdbc)

    1.介绍 对mysql.oracle等数据库数据进行同步到ES有三种做法:一个是通过elasticsearch提供的API进行增删改查,一个就是通过中间件进行数据全量.增量的数据同步,另一个是通过收集 ...

  8. [c++]堆和栈的区别

    堆和栈的区别一.预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构 ...

  9. npm package 装包匹配原则

    经常看到package.json 里面有这样的devDependencies: "devDependencies": { "@angular/common": ...

  10. C#多线程开发

    1.进程与线程的区别 通俗的讲,进行就是任务管理器中进行列表中看到的正在运行的程序,它是一个动态的概念,活动的实体. 线程是程序执行流的最小单元,是线程中一个实体,是系统独立调度和分派CPU基本单位. ...