POJ 3268 Silver Cow Party

2015年7月17日。
POJ 3268 Silver Cow Party
题目大意:有N(1 <= N <= 1000)头牛,编号从1到N,分布在一张图上,他们要到X(1 <= X <= N)牛哪里去参加part,所以共有N - 1头牛需要移动,这N头牛之间有M条路连接起来,牛Ai到牛Bi所花费的时间是Ti,即编号为Ai的牛到编号为Bi的牛需要花Ti单位时间,注意这是单向边。问所有牛到X参加part,然后再回到原来的地方,从0时刻起,到最后一头牛回到原来的地方的时刻。也就是说,最慢的牛去参加part再回到自己原来的地方所需要的时间。
明显的最短路,将所有两头牛的最短距离求出,再求出其他牛到X牛所花费的最大时间。用dijkstra算法,加个优先队列维护即可。时间复杂度 O(|E||V|log|V|)

贴出代码,仅供参考。

/*************************************************************************
    > File Name: 3268.cpp
    > Author: Royecode
    > Email: Royecode@163.com 
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define MAXN 1001
#define MAXM 100007
#define INF 1e7
#define P pair <int, int>
using namespace std;
struct edge
{
    int to, cost;
    edge(int _to, int _cost):to(_to), cost(_cost)
    {}
};
vector <edge> side[MAXM];
int d[MAXN][MAXN];
//求出n这头牛,到其他牛的最短距离
void dijkstra(int v, int n)
{
    priority_queue <P, vector <P>, greater<P> > que;
    fill(d[n], d[n] + MAXN, INF);
    d[n][n] = 0;
    que.push(P(0, n));
    while(!que.empty())
    {
        P e = que.top(); que.pop();
        int v = e.second;
        if(d[n][v] < e.first) continue;
        for(int i = 0; i < side[v].size(); ++i)
        {
            edge s = side[v][i];
            if(d[n][s.to] > d[n][v] + s.cost)
            {
                d[n][s.to] = d[n][v] + s.cost;
                que.push(P(d[n][s.to], s.to));
            }
        }
    }
}
int main()
{
    int n, m, s;
    scanf("%d%d%d", &n, &m, &s);
    for(int i = 0; i < m; ++i)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        side[a].push_back(edge(b, t));
    }
    for(int i = 1; i <= n; ++i)
        dijkstra(n, i);
    int ans = 0;
    //求出最大的时间
    for(int i = 1; i <= n; ++i)
        ans = max(ans, d[i][s] + d[s][i]);
    printf("%d\n", ans);
    return 0;
}
/*
Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output

10
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值