HDU 3268 Columbus’s bargain

原创 2013年12月01日 23:11:13

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

题解:这题真是老太婆的裹脚布--又臭又长。反正我第一次死活A不了的原因是题没理解清楚。题意我copy一下别人的吧,我自己表达不清楚,我也是看了别人写的题意才改了一点地方才AC的...题目大意:要买n种东东,每种东东有一个价格。可以用4种方式买这些东东:1、直接用金币全额购买(没用的条件……);2、若东东k元,付k-1个金币加一个珠子买。;3、用一个东东加一些钱交换另一个东东。4:等价值的东东可以交换。问每个东东最少用多少钱可以卖到,有几个东东满足其实付等于另外两个东东的实付之和。并且这几个东西是单独的,怎么说呢?也就是A=B+C,这算一种,就算是C+D=A但是前面已经有满足的A了,这不算一种!!!好吧,我就错这了。。。我开始是用Map存起来算的。。。

AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
int a[100],vis[100],d[100][100],n,m;
void ff(int k)
{
	int i;
	for (i=1;i<=n;i++)
		if ( d[i][k]!=-1 )
		{
			if (vis[i]==0)
			{
				vis[i]=1;
				ff(i);
			}
			a[k]=min(a[k],a[i]+d[i][k]);
		}
}
int main()
{
	int T,i,j;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d",&n);
		memset(d,-1,sizeof(d));
		memset(vis,0,sizeof(vis));
		for (i=1;i<=n;i++)
		{
			scanf("%d%d",&j,&a[i]);
			a[i]--;
			for (j=1;j<i;j++)
				if (a[i]==a[j]) d[i][j]=d[j][i]=0;
		}
		scanf("%d",&m);
		for (i=0;i<m;i++)
		{ 
			int q,w,v;
			scanf("%d%d%d",&q,&w,&v);
			if (d[q][w]>v || d[q][w]==-1)
				d[q][w]=v;
		}
		for (i=1;i<=n;i++)
			if (vis[i]==0)
			{
				vis[i]=1;
				ff(i);
			}
		for (i=1;i<=n;i++)
		{
			printf("%d %d\n",i,a[i]);
		}
		int res=0;
        bool flag=false;
        for(int i=1;i<=n;i++){
            flag=false;
            for(int j=1;j<=n;j++){
                if(j==i) continue;
                for(int k=j+1;k<=n;k++){
                    if(k==i) continue;
                    if(a[i]==a[j]+a[k]){
                         flag=true;
                    }
                    if(flag) break;
                }
                if(flag) break;
            }
            if(flag) res++;
        }
		printf("%d\n",res);
	}
	return 0;
}





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

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

Columbus’s bargain (hdu 3268 最短路)

题意:n个物品,每个物品价值为pi,可以用一个玻璃珠和pi-1个金币换的,也可以由其他便宜的物品加上一定量的金币换得,相同价格的物品可以互换。问每个物品通过这样交换若干次后最少要花的金币数是多少。并且...

HDU 3268 Columbus’s bargain

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

从Gaussian 09的Hartree-Fock计算的fchk文件转换到Columbus的mocoef文件的脚本

这个脚本用于将Gaussian 09的Hartree-Fock计算的.fchk文件转换到Columbus软件使用的mocoef文件。...

POJ 3835 Columbus's bargain

题目大意: 题目链接 zhus

POJ 3835 Columbus's bargain(最短路)

Columbus's bargain Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 721   Accepted...

POJ 3835 Columbus's bargain 最短路

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

灵通lt-3268

  • 2014年12月04日 12:19
  • 2.91MB
  • 下载

Columbus--UsersGuide

  • 2009年04月09日 17:35
  • 1.1MB
  • 下载

HDU1845 Jimmy’s Assignment 984ms 飘过啊 汗死 二分匹配最大匹配数

984ms飘过,其实就是卡过去的,纯属看人品的 代码,使用了邻接表,更快点,这道题目 他的匹配点 是自己的,所以以前 那个标记 点是否被连过的数组 要设两个,一个表示左集中的点 一个在右集中的点, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 3268 Columbus’s bargain
举报原因:
原因补充:

(最多只允许输入30个字)