Columbus ' s bargainTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1836 Accepted Submission (s): 467
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 < ; =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
Source2009 Asia Ningbo Regional Contest Hosted by NIT
Recommendlcy | We have carefully selected several similar problems for you:3265 3264 3262 3269 3263
Test instructions: n items, each item value is PI, can be exchanged with a glass bead and pi-1 gold coins, can also be replaced by other cheap items plus a certain amount of gold coins, the same price items can be exchanged. Ask what the minimum number of coins to spend on each item is to be exchanged several times. And how many items actual price equals the sum of the other two items actual price, note that each item is counted as one if it has multiple combinations.
Code:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;co NST int MAXN = 2005;const int MAXM = 200010;typedef pair<int,int> p;struct edge{int v,len,next;} Edge[maxm];int a[maxn];int head[maxn],dist[maxn];int num,n,m;int vis[maxn];void init () {num=0; memset (head,-1,sizeof (Head));} void Addedge (int u,int v,int W) {edge[num].v=v; Edge[num].len=w; Edge[num].next=head[u]; head[u]=num++;} void Dijkstra () {priority_queue<p,vector<p>,greater<p> >Q; memset (dist,inf,sizeof (Dist)); Q.push (P (0,0)); dist[0]=0; while (! Q.empty ()) {P p=q.top (); Q.pop (); int U=p.second; if (Dist[u]<p.first) continue; for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v; if (Dist[v]>dist[u]+edge[i].len) {Dist[v]=dist[u]+edge[i].len; Q.push (P (dist[v],v)); }}} for (int i=1;i<=n;i++) printf ("%d%d\n", i,dist[i]); int ans=0; memset (vis,0,sizeof (VIS)); There are several combinations of each item that are counted as one, with the VIS to mark for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (i==j) continue; for (int k=j+1;k<=n;k++) {if (i==k| | J==K) continue; if (Dist[i]==dist[j]+dist[k]&&!vis[i]) {ans++; Vis[i]=1; }}}} printf ("%d\n", ans);} int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j,t,u,v,w; scanf ("%d", &t); while (t--) {init (); scanf ("%d", &n); for (i=1;i<=n;i++) {scanf ("%d%d", &u,&a[i]); a[i]--; Addedge (0,u,a[i]); } for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (A[i]==a[j]) Addedge (i,j,0); }} scanf ("%d", &m); while (m--) {scanf ("%d%d%d", &u,&v,&amP;W); Addedge (U,V,W); } Dijkstra (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Columbus ' s bargain (hdu 3268 shortest)