Columbus’s bargain

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

2009
Asia Ningbo Regional Contest Hosted by NIT

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<queue>
  6 using namespace std;
  7 const int ms=25;
  8 const int inf=0xffffff;
  9 struct edge
 10 {
 11     int u,v,w,next;
 12 }edges[ms*ms];
 13 int head[ms],price[ms],dis[ms],cnt,n,m;
 14 bool  vis[ms];
 15 void add_edge(int u,int v,int w)
 16 {
 17     edges[cnt].u=u;
 18     edges[cnt].v=v;
 19     edges[cnt].w=w;
 20     edges[cnt].next=head[u];
 21     head[u]=cnt++;
 22     return ;
 23 }
 24
 25 void input()
 26 {
 27     int i,j,id,pri;
 28     memset(vis,false,sizeof(vis));
 29     memset(head,-1,sizeof(head));
 30     fill(dis,dis+ms,inf);
 31     cnt=0;
 32     scanf("%d",&n);
 33     for(i=0;i<n;i++)
 34     {
 35         scanf("%d %d",&id,&pri);
 36         price[id]=pri;
 37         add_edge(0,id,pri-1);
 38     }
 39     for(i=1;i<=n;i++)
 40         for(j=i+1;j<=n;j++)
 41             if(price[i]==price[j])
 42     {
 43
 44         add_edge(i,j,0);
 45         add_edge(j,i,0);
 46     }
 47     scanf("%d",&m);
 48     while(m--)
 49     {
 50         scanf("%d %d %d",&i,&j,&pri);
 51         add_edge(i,j,pri);
 52     }
 53     return ;
 54 }
 55 void spfa()
 56 {
 57    int i,s=0,to;
 58    queue<int> que;
 59    dis[s]=0;
 60    que.push(s);
 61    vis[s]=true;
 62    while(!que.empty())
 63    {
 64       // s=que.top();  栈
 65        s=que.front();
 66        que.pop();
 67        for(i=head[s];i!=-1;i=edges[i].next)
 68        {
 69            to=edges[i].v;
 70            if(dis[to]>dis[s]+edges[i].w)
 71            {
 72                dis[to]=dis[s]+edges[i].w;
 73                if(!vis[to])
 74                {
 75                    vis[to]=true;
 76                    que.push(to);
 77                }
 78            }
 79        }
 80        vis[s]=false;
 81    }
 82    return ;
 83 }
 84 void solve()
 85 {
 86     int i,j,k,ans=0;
 87     spfa();
 88     for(i=1;i<=n;i++)
 89         printf("%d %d\n",i,dis[i]);
 90     bool flag;
 91     for(i=1;i<=n;i++)
 92         for(flag=true,j=1;j<=n&&flag;j++)
 93             for(k=j+1;k<=n&&flag;k++)
 94                 if(i!=j&&i!=k)
 95     {
 96         if(dis[i]==dis[j]+dis[k])
 97         {
 98             ans++;
 99                flag=false;
100         }
101     }
102     printf("%d\n",ans);
103     return ;
104 }
105 int main()
106 {
107     int T;
108     scanf("%d",&T);
109     while(T--)
110     {
111         input();
112         solve();
113     }
114     return 0;
115 }

时间: 2024-10-20 19:40:08

Columbus’s bargain的相关文章

Columbus’s bargain (hdu 3268 最短路)

Columbus's bargain Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1836    Accepted Submission(s): 467 Problem Description On the evening of 3 August 1492, Christopher Columbus departed from Pa

HDU 3268 Columbus’s bargain

Columbus's bargain Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1829    Accepted Submission(s): 465 Problem Description On the evening of 3 August 1492, Christopher Columbus departed from Pa

HDU 3268 Columbus’s bargain(最短路 Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3268 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 k

hdu 3268 09 宁波 现场 I - Columbus’s bargain

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

BARGAIN BIN FREE AGENTS DALLAS COWBOYS SHOULD TARGET TO FILL BIGGEST NEEDS

With the new league year beginning on March 9, the NFL's free-agency period is in full swing with the legal tampering period starting<a href="http://www.authenticcowboyssale.com/shop-by-players-anthony-brown-jersey-c-2_69.html">Anthony Bro

&lt;老友记&gt;学习笔记

这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的情路坎坷,事业成败和生活中的喜怒哀乐,无时无刻不牵动着彼此的心,而正是正平凡的点点滴滴,却成为最令人感动与留恋的东西. 人物:1.瑞秋•格林(RACHEL GREENE)由珍妮佛•安妮斯顿(Jennifer Aniston)扮演 瑞秋是莫妮卡的高中同学,在与牙医未婚夫的婚礼上脱逃至莫妮卡处. 2.罗

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M

世界主要城市经纬度

城市英文名 城市中文名 所属国家 纬度 经度 Abidjan 阿比让 科特迪瓦 北纬:5°19' 东经:4°01' Abu Dhabi 阿布扎比 阿联酋 北纬:24°27' 东经:54°23' Abuja 阿布贾 尼日利亚 北纬:9°12' 东经:7°11' Acapulco 阿卡普尔科 墨西哥 北纬:16°51' 西经:99°56' Accra 阿克拉 加纳 北纬:5°33' 东经:0°15' Adak 艾达克岛 美国 北纬:51°52' 东经:176°39' Adamstown 亚当斯敦 英