HDU 3268 Columbus ' s bargain

Source: Internet
Author: User

Columbus ' s bargainTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1829 Accepted Submission (s): 465


Problem Descriptionon the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera with a few Shi PS, 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 is not large enough and there is seldom harbors in he route, Columbus had to buy food and other neces Sary 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 have a price in gold coins. Columbus brought enough glass beads with him, because he knew "for savages", a glass bead is as valuable as a gold coin . Columbus could buy a item he need only in four ways below:

1. Pay the price of all by gold coins.
2. Pay by one glass bead and some gold coins. In the this to, if an item's price was K gold coins, Columbus could just pay k–1 gold coins and one glass bead.
3. Pay by an item which have the same price.
4. Pay by a cheaper item and some gold coins.

Columbus found out a interesting thing in the trade rule of savages:for some kinds of goods, when the buyer wanted to bu Y an item by paying a cheaper item and some gold coins, he didn ' t has to pay the price difference, he can pay less. If one could buy a 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 is a "bargain" between kind B and kind A. To get an item, Columbus didn ' t has to spend gold coins as many as it price because he could use glass beads or took ful L 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–colu MBus called it "actual price" of that kind of goods.

Just for Curiosity, Columbus also wanted to know, how many kinds of goods is there whose "actual price" is equal to the Sum of "actual price" of the other kinds.

Inputthere is several test cases.
The first line, 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 is n kinds of goods (0 < N <= 20). These n kinds is numbered from 1 to N.

Then N lines follow, each contains a integers q and p, meaning the price of the goods of kind Q is P. (0 <q &lt ; =n, 0 < P <= 30)
The next line is an integer m (0 < M <=), meaning there is M "bargains".

Then M lines follow, each contains three integers N1, N2 and R, meaning so can get a item of kind N2 by paying an I TEM 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 are none negative and less than the PR Ice difference between the goods of kind N2 and kind N1. Please note that R could is zero.

Outputfor each test case:
Please output N lines at first. Each line contains-integers n and p, meaning that the ' actual price ' of the goods of kind n is P gold coins. These N lines should is in the ascending order of kind No.

Then output a line containing an integer m, indicating that there be m kinds of goods whose "actual price" was equal to th E sum of "actual price" of other kinds.

Sample Input
141 42 93 54 1321 2 33 4 6

Sample Output
1 32 63) 44 101


Test instructions: Tell n items and the amount of gold required to buy each item, for each item, can and can only use a glass ball to replace a gold coin, equivalence between the items can be exchanged, tell A,b,c three number, B items can use a item plus C gold coins to exchange, The minimum number of gold coins required for each shipment and how many goods can be replaced by two other goods.

Idea: can be abstracted as the shortest-circuit problem, each cargo as an end point, set a 0 points, 0 to I of the weight is the price of the first item, so you can use Floyd to solve the shortest way to find the lowest price per endpoint


#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <queue> #define INF 99999999using namespace Std;int t;int val;int mp[1009][1009];int Main () {scanf ("%    D ", &t);    int k;    int n;    int a,b,c;       while (t--) {scanf ("%d", &n);               for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) {if (i==j) mp[i][j]=0;           Mp[i][j]=inf;            } for (int i=1;i<=n;i++) {scanf ("%d%d", &k,&val);        MP[0][I]=VAL-1;        } scanf ("%d", &k);            for (int i=0;i<k;i++) {scanf ("%d%d%d", &a,&b,&c);        Mp[a][b]=c;                } for (int i=0;i<=n;i++)//exists equivalent exchange for (int j=0;j<=n;j++) {if (Mp[0][i]==mp[0][j])        mp[i][j]=mp[j][i]=0; } for (int k=0;k<=n;k++) for (int. i=0;i<=n;i++) for (int j=0;j<=n;j++) mp[i][j]=min (Mp[i][j],mp[i][k]+mp[k][j]);        for (int i=1;i<=n;i++) printf ("%d%d\n", i,mp[0][i]);        int ans=0;            for (int i=1;i<=n;i++) {int flag=0;                for (int j=1;j<=n;j++) {if (i==j) continue;                    for (int k=1;k<=n;k++) {if (J==k | | i==k) continue; if (Mp[0][i]==mp[0][j]+mp[0][k]) {flag=1;//cout<< "                    I= "<<i<<";//cout<< "j=" <<j<< "<<" k= "<<k<<endl;        }}} if (flag) ans++;    } printf ("%d\n", ans); } return 0;}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3268 Columbus ' s bargain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.