ACM篇:POJ 1125 --Stockbroker Grapevine

枚举起点。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#define max(a,b) (((a) > (b)) ? (a) : (b))
using namespace std;

const int MAX = 100;
const int SUP = 999999999;

int find_max(int *begin, int *end)
{
    int ret = -SUP;
    int *p = begin;
    while (p != end)
        {
            ret = max(ret, *p);
            p++;
        }
    return ret;
}
struct Road
{
    int des;
    int cost;
    Road(int des=0, int cost=0)
    {
        this->des = des;
        this->cost = cost;
    }
};
vector<Road> road[MAX+2];

int n;
int ans;
int start;
int pass[MAX+2];
bool visit[MAX+2];
queue<int> q;
void read_map()
{
    for (int i = 1; i <= n; i++)
        road[i].clear();

    for (int i = 1; i <= n; i++)
    {
        int sz;
        scanf("%d", &sz);
        for (int j = 1; j <= sz; j++)
        {
            int des, cost;
            scanf("%d%d", &des, &cost);
            road[i].push_back(Road(des, cost));
        }
    }
}

void _bfs(int x)
{
    for (int i = 1; i <= n; i++)
        pass[i] = SUP;
    while (!q.empty())
        q.pop();
    memset(visit, 0, sizeof(visit));

    int head;
    int cnt = 0;
    q.push(x);
    pass[x] = 0;

    while (!q.empty())
    {
        head = q.front();
        q.pop();
        if (!visit[head])
        {
            visit[head] = true;
            cnt++;
        }

        int sz = road[head].size();
        for (int i = 0; i < sz; i++)
        {
            if (pass[road[head][i].des] > pass[head] + road[head][i].cost)
            {

                pass[road[head][i].des] = pass[head] + road[head][i].cost;

                q.push(road[head][i].des);
            }
        }
    }

    if (cnt == n)
    {
        int max_cost = find_max(pass+1, pass + n+1);
        if (max_cost < ans)
        {
            ans = max_cost;
            start = x;
        }
    }
}

void _print()
{
    if (ans != SUP)
        printf("%d %d\n", start, ans);
    else
        printf("disjoint\n");
}
int main()
{
    while (scanf("%d", &n) && n)
    {
        read_map();

        ans = SUP;
        for (int i = 1; i <= n; i++)
            _bfs(i);

        _print();
    }   
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值