Columbus ' s bargain

Source: Internet
Author: User

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 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 Input141 42 93 54 1321 2 33 4 6

Sample OUTPUT1 32 63 44 101

Source2009 Asia Ningbo Regional Contest Hosted by NIT
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intms= -;8 Const intinf=0xFFFFFF;9 structEdgeTen { One     intU,v,w,next; A}edges[ms*MS]; - inthead[ms],price[ms],dis[ms],cnt,n,m; - BOOLVis[ms]; the voidAdd_edge (intUintVintW) - { -edges[cnt].u=u; -edges[cnt].v=v; +edges[cnt].w=W; -edges[cnt].next=Head[u]; +head[u]=cnt++; A     return ; at } -  - voidinput () - { -     intI,j,id,pri; -memset (Vis,false,sizeof(Vis)); inmemset (head,-1,sizeof(head)); -Fill (dis,dis+ms,inf); toCnt=0; +scanf"%d",&n); -      for(i=0; i<n;i++) the     { *scanf"%d%d",&id,&pri); $price[id]=pri;Panax NotoginsengAdd_edge (0, id,pri-1); -     } the      for(i=1; i<=n;i++) +          for(j=i+1; j<=n;j++) A             if(price[i]==Price[j]) the     { +  -Add_edge (I,j,0); $Add_edge (J,i,0); $     } -scanf"%d",&m); -      while(m--) the     { -scanf" %d%d%d",&i,&j,&pri);Wuyi Add_edge (I,J,PRI); the     } -     return ; Wu } - voidSPFA () About { $    inti,s=0, to; -queue<int>que; -dis[s]=0; - Que.push (s); Avis[s]=true; +     while(!que.empty ()) the    { -       //S=que.top (); Stack $s=Que.front (); the Que.pop (); the         for(i=head[s];i!=-1; i=edges[i].next) the        { theto=edges[i].v; -            if(dis[to]>dis[s]+EDGES[I].W) in            { thedis[to]=dis[s]+EDGES[I].W; the                if(!Vis[to]) About                { thevis[to]=true; the Que.push (to); the                } +            } -        } thevis[s]=false;Bayi    } the    return ; the } - voidSolve () - { the     inti,j,k,ans=0; the SPFA (); the      for(i=1; i<=n;i++) theprintf"%d%d\n", I,dis[i]); -     BOOLFlag; the      for(i=1; i<=n;i++) the          for(flag=true, j=1; j<=n&&flag;j++) the              for(k=j+1; k<=n&&flag;k++)94                 if(i!=j&&i!=k) the     { the         if(dis[i]==dis[j]+Dis[k]) the         {98ans++; Aboutflag=false; -         }101     } 102printf"%d\n", ans);103     return ;104 } the intMain ()106 {107     intT;108scanf"%d",&T);109      while(t--) the     {111 input (); the solve ();113     } the     return 0; the}
View Code

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.