码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3268 09 宁波 现场 I - Columbus’s bargain

时间:2014-11-11 18:51:32      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   os   sp   for   div   on   

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: 

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
 
spfa,认真读题,第四种情况是给定的,坑了一整场
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxm=50001;
const int maxn=51;
struct edge{
    int to,cost,nxt;
}e[maxm];
int first[maxn],price[maxn],m,n,len;
void addedge(int f,int t,int c){
    e[len].to=t;e[len].cost=c;e[len].nxt=first[f];
    first[f]=len++;
}
void debug(){
    for(int i=1;i<=n;i++){
        for(int p=first[i];p!=-1;p=e[p].nxt){
            int t=e[p].to;
            printf("f %d t %d c %d\n",i,t,e[p].cost);
        }
    }
}
queue<int>que;
void spfa(){
    for(int i=1;i<=n;i++)que.push(i);
    while(!que.empty()){
        int f=que.front();que.pop();
        for(int p=first[f];p!=-1;p=e[p].nxt){
            int t=e[p].to;
            if(price[t]>price[f]+e[p].cost){
                price[t]=price[f]+e[p].cost;
                que.push(t);
            }
        }
    }
}
int main(){
    int caT;
    scanf("%d",&caT);
    while(caT--){
        memset(first,-1,sizeof(first));
        memset(price,0x3f,sizeof(price));
        scanf("%d",&n);
        int f,t,c;
        len=0;
        for(int i=0;i<n;i++){scanf("%d%d",&f,&c);price[f]=c;}
        scanf("%d",&m);
        for(int i=0;i<m;i++){scanf("%d%d%d",&f,&t,&c);if(f!=t)addedge(f,t,c);}
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j&&price[i]==price[j]){addedge(j,i,price[i]-price[j]);addedge(i,j,price[i]-price[j]);}
        for(int i=1;i<=n;i++)if(price[i]>0)price[i]--;
        //debug();
        spfa();
        for(int i=1;i<=n;i++)printf("%d %d\n",i,price[i]);
        int ans=0;
        for(int i=1;i<=n;i++){
            bool fl=false;
            for(int j=1;j<=n;j++){
                for(int k=j+1;k<=n;k++){
                    if(i!=j&&i!=k&&price[j]+price[k]==price[i]){fl=true;break;}
                }
                if(fl)break;
            }
            if(fl)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

hdu 3268 09 宁波 现场 I - Columbus’s bargain

标签:des   blog   io   ar   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/xuesu/p/4089976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!