Columbus's bargain
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 721 | Accepted: 203 |
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:
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.
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:
- Pay the price all by gold coins.
- 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.
- Pay by an item which has the same price.
- 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.
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.
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
题目大意
先输入T组测试数据输入N个物品每个物品的种类Q和价值V输入M个关系A换B花C个金币四种换法1、全价买(这种没用)2、用1个水晶和价值V-1个金币 (从起点到这个物品 添一条权值V-1的单向边)3、物品价值相等 时物物交换 (将两个物品之间添一条权值为0的双向边)4、物物价值不等时交换需要花C金币 (将A到B添一条权值为C的双向边)本题可用Dijkstra , Floyd, bellman-ford, spfa求解,这里给出了Dijkstra的解法本题有几个要注意的地方1.价值相同的物品间也要建边,并且边权值为02.最后计数时不要重复计数
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <string> #include <queue> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; const int maxn = 105; int n, m, cnt, dis[maxn], vis[maxn], val[maxn]; int mp[maxn][maxn]; void dijkstra() { for(int i = 1;i <= n;++i){ vis[i] = 0; dis[i] = mp[0][i]; } dis[0] = 0; for(int i = 1;i <= n;++i) { int minl = inf, t = 0; for(int j = 1;j <= n;++j) { if(!vis[j] && dis[j] < minl) { t = j; minl = dis[j]; } } vis[t] = 1; for(int j = 1;j <= n;++j) { if(!vis[j] && mp[t][j] < inf) { if(dis[j] > dis[t] + mp[t][j]) { dis[j] = dis[t]+mp[t][j]; } } } } } int main() { int T, a, b, c; cin>>T; while(T--) { scanf("%d",&n); memset(mp,0x3f,sizeof(mp)); for(int i = 0;i < n;++i) { scanf("%d%d",&a,&b); mp[0][a] = b-1; val[i] = b; } scanf("%d",&m); for(int i = 0;i < m;++i) { scanf("%d%d%d",&a,&b,&c); mp[a][b] = c; } for(int i=1;i<n;i++) { for(int j=i+1;j<=n;j++) { if(mp[0][i]==mp[0][j]) { mp[i][j]=mp[j][i]=0; } } } dijkstra(); int ans = 0; bool flag; for (int i=1;i<=n;++i) { flag=0; for (int j=1;j<=n;++j) if (i!=j) for (int k=1;k<=n;++k) if (i!=k && j!=k) if (dis[j]+dis[k]==dis[i]) flag=1; if (flag) ++ans; } for(int i = 1;i <= n;++i) printf("%d %d\n",i,dis[i]); printf("%d\n",ans); } return 0; }
Floy代码
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <string> #include <queue> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; const int maxn = 105; int n, m, cnt, dis[maxn], vis[maxn], val[maxn]; int mp[maxn][maxn]; int main() { int T, a, b, c; cin>>T; while(T--) { scanf("%d",&n); memset(mp,0x3f,sizeof(mp)); for(int i = 0;i < n;++i) { scanf("%d%d",&a,&b); mp[0][a] = b-1; val[i] = b; } scanf("%d",&m); for(int i = 0;i < m;++i) { scanf("%d%d%d",&a,&b,&c); mp[a][b] = c; } for(int i=1;i<n;i++) { for(int j=i+1;j<=n;j++) { if(mp[0][i]==mp[0][j]) { mp[i][j]=mp[j][i]=0; } } } bool flag; do { flag=false; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { for(int k=0;k<=n;k++) { if(mp[i][j]+mp[j][k] < mp[i][k]) { mp[i][k] = mp[i][j] + mp[j][k]; flag=true; } } } } }while(flag); int ans = 0; for (int i=1;i<=n;++i) { flag=0; for (int j=1;j<=n;++j) if (i!=j) for (int k=1;k<=n;++k) if (i!=k && j!=k) if (mp[0][j]+mp[0][k]==mp[0][i]) flag=1; if (flag) ++ans; } for(int i = 1;i <= n;++i) printf("%d %d\n",i,mp[0][i]); printf("%d\n",ans); } return 0; }