Stockbroker Grapevine (POJ 1125)

Description

Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way. 

Unfortunately for you, stockbrokers only trust information coming from their "Trusted sources" This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.

Input

Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a '1' means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules. 

Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people. 

Output

For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes. 
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message "disjoint". Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.

Sample Input

3
2 2 4 3 5
2 1 2 3 6
2 1 2 2 2
5
3 4 4 2 8 5 3
1 5 8
4 1 6 4 10 2 7 5 2
0
2 2 5 1 5
0

Sample Output

3 2
3 10
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;

//感谢周洋师弟的帮助;
//题目比较难懂;
//第一行,经纪人个数;
//第二行,一号经纪人的朋友数、朋友编号、消息传播速度……;

const int MAX_V = 110;
const int INF = 1 << 25;
// 设为1 << 30会溢出;
int dist[MAX_V][MAX_V];
int min_dist[MAX_V];
int n;
void floyd()
{
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}
int main()
{
    cin >> n;
    while (n != 0)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dist[i][j] = INF;
            }
            dist[i][i] = 0;
            min_dist[i] = -1;
        }
        for (int i = 1; i <= n; i++)
        {
            int m;
            cin >> m;
            for (int j = 1; j <= m; j++)
            {
                
                int a, b;
                cin >> a >> b;
                dist[i][a] = b;
            }
        }
        floyd();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (i != j)
                {
                    min_dist[i] = max(min_dist[i], dist[i][j]);
                }
            }
        }
        int num = min_element(min_dist + 1, min_dist + n + 1) - min_dist;
        int min_time = min_dist[num];
        if (min_time != INF)
            cout << num << " " << min_time << endl;
        else
            cout << "disjoint" << endl;
        cin >> n;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
股票经纪人问题是一个经典的贪心算法问题。其描述为:有n个客户想要买入或卖出股票,经纪人需要在市场上进行交易,并且每次交易需要支付一定的手续费。现在给定每个客户的买入或卖出股票数量、价格和手续费,求经纪人的最大收益。 Java代码实现如下: ```java public class StockBroker { public static void main(String[] args) { int[] buy = {5, 2, 4}; int[] sell = {4, 3, 5}; int[] price = {2, 3, 1}; int[] fee = {1, 3, 2}; int maxProfit = getMaxProfit(buy, sell, price, fee); System.out.println("Max Profit: " + maxProfit); } public static int getMaxProfit(int[] buy, int[] sell, int[] price, int[] fee) { int n = buy.length; int[] sellProfit = new int[n]; int[] buyProfit = new int[n]; int maxSellProfit = 0; int maxBuyProfit = 0; // 计算卖出每个客户的最大收益 for (int i = 0; i < n; i++) { sellProfit[i] = sell[i] * price[i] - fee[i]; maxSellProfit = Math.max(maxSellProfit, sellProfit[i]); } // 计算买入每个客户的最大收益 for (int i = 0; i < n; i++) { buyProfit[i] = buy[i] * price[i] - fee[i] - maxSellProfit; maxBuyProfit = Math.max(maxBuyProfit, buyProfit[i]); } return maxBuyProfit + maxSellProfit; } } ``` 在这段代码中,我们先定义了一个`StockBroker`类,其中包含一个`getMaxProfit`方法用于计算经纪人的最大收益。在`main`方法中,我们定义了一个示例输入,然后调用`getMaxProfit`方法并输出结果。 在`getMaxProfit`方法中,我们首先定义了一些变量,包括每个客户卖出股票的最大收益数组`sellProfit`、每个客户买入股票的最大收益数组`buyProfit`,以及卖出股票的最大收益`maxSellProfit`和买入股票的最大收益`maxBuyProfit`。接着,我们通过循环计算每个客户卖出股票和买入股票的最大收益,并更新相应的最大收益变量。最后,我们将卖出和买入的最大收益相加并返回结果。 该算法的时间复杂度为O(n),空间复杂度为O(n)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值