Columbus’s bargain (hdu 3268 最短路)

Columbus’s bargain

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1836    Accepted Submission(s): 467

Problem 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

Source

2009 Asia Ningbo Regional Contest Hosted by NIT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3265 3264 3262 3269 3263

题意:n个物品,每个物品价值为pi,可以用一个玻璃珠和pi-1个金币换的,也可以由其他便宜的物品加上一定量的金币换得,相同价格的物品可以互换。问每个物品通过这样交换若干次后最少要花的金币数是多少。并且有多少物品的actual price等于其他两个物品actual
price之和,注意每个物品如果有多个组合形式也只算作一种。

代码:

#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 1000000009
const int maxn = 1005;
const 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));      //每种物品有多种组合方式也只算作一种,用vis来标记
    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,&w);
            addedge(u,v,w);
        }
        Dijkstra();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-04 05:19:07

Columbus’s bargain (hdu 3268 最短路)的相关文章

HDU 3268 Columbus’s bargain

Columbus's bargain Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1829    Accepted Submission(s): 465 Problem Description On the evening of 3 August 1492, Christopher Columbus departed from Pa

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

Columbus’s bargain

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 Description On the evening of 3 August 1492, Christopher Columbus departed from Pal

hdu 2112 (最短路+map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14515    Accepted Submission(s): 3405 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发

HDU 2544 最短路(我的dijkstra算法模板、SPAFA算法模板)

思路:这道题是基础的最短路径算法,可以拿来试一下自己对3种方法的理解 dijkstra主要是从第一个点开始枚举,每次枚举出当当前最小的路径,然后再以那最小的路径点为起点,求出它到其它未标记点的最短距离 bellman-ford 算法则是假设有向网中有n 个顶点.且不存在负权值回路,从顶点v1 和到顶点v2 如果存在最短路径,则此路径最多有n-1 条边.这是因为如果路径上的边数超过了n-1 条时,必然会重复经过一个顶点,形成回路:而如果这个回路的权值总和为非负时,完全可以去掉这个回路,使得v1到v

hdu 4849 最短路 西安邀请赛 Wow! Such City!

http://acm.hdu.edu.cn/showproblem.php?pid=4849 会有很多奇怪的Wa的题,当初在西安就不知道为什么wa,昨晚做了,因为一些Sb错误也wa了很久,这会儿怎么写都会AC.... 收获: 1.还是基本都构思好在去敲代码,因为当时没过,昨晚心里有阴影,敲得很慢,而且最开始各种取模以防止漏掉,太保守了......以后一定先估算是不是需要取模防止TLE,当然时间够的话还是适当多取个模防止莫名其妙的错误.. 2.如果出错,注意参数是不是对的,最开始写好之后,因为m和

hdu 5521 最短路

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1656    Accepted Submission(s): 515 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Joh

hdu 2544 最短路 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需要的最短时间是多少. 这题是最短路的入门题,从理解d-i--j---k(wg自创的,呵呵)到默打到修改,搞左两日终于好了,哈哈哈~~~太感动了. 第一次错是 少了Dijkstra()函数中的 for (j = 1; j <= n; j++) . 第二次错是把vis[k=j]=1 写在了 if (!v

hdu 1595 最短路

题意是要我们求出最短路中一条路坏的情况下最大的时间: 我们先将最短路求出并记录路径,然后枚举最短路中每一条路坏的情况,求出最大的时间.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<cmath> using namespace st