USACO3.2.5-butter

2017年5月16日 | ljfcnyali
题目大意
农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他用额外赚来的钱给奶牛买奢侈品。
农夫John很狡猾。他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。
农夫John知道每只奶牛都在各自喜欢的牧场呆着(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

Sample Input

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

Sample Output

8

样例图形

         P2  
P1 @--1--@ C1
    \    |\
     \   | \
      5  7  3
       \ |   \
        \|    \ C3
      C2 @--5--@
         P3    P4

题目分析
枚举每一个终点,Dijstra进行单源最短路径,因为这样子会超时,所以加上一个堆优化即可。

题目注意
输出要换行。

AC代码

/*
LANG: C++
ID: jerry272
PROG: butter
*/

/*************************************************************************
    > File Name: USACO3.2.5-butter.cpp
    > Author: ljf-cnyali
    > Mail: ljfcnyali@gmail.com 
    > Created Time: 2017/5/16 19:20:19
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int inf = 1 << 30;
const int maxn = 810;

struct Edge{
    int from, to, dist;
} ;

struct Node{
    int u, d;
    bool operator < (const Node& n) const{
        return d > n.d;
    }
};

vector<Edge> edges;

int d[maxn], cnt[maxn], v[maxn];
int N, M, C;

vector<int> G[maxn];

void Dijksta(int s) {
    REP(i, 1, N)
        d[i] = inf, v[i] = 0;
    d[s] = 0;
    priority_queue<Node> Q;
    Q.push((Node) { s, 0});
    while ( !Q.empty()){
        Node x = Q.top();
        Q.pop();
        int u = x.u;
        v[u] = 1;
        REP(i, 0, G[u].size() - 1) {
            Edge& e = edges[G[u][i]];
            if(v[e.to]) continue;
            if(d[u] + e.dist < d[e.to]) {
                d[e.to] = d[u] + e.dist;
                Q.push((Node){e.to, d[e.to]});
            }
        }
    }

} 

int main() {
    freopen("butter.in", "r", stdin);
    freopen("butter.out", "w", stdout);
    mem(cnt);
    int cow, from, to, dist;
    cin >> C >> N >> M;
    while(C --) {
        cin >> cow;
        cnt[cow] ++;
    }
    while(M--) {
        cin >> from >> to >> dist;
        edges.push_back((Edge){from, to, dist});
        edges.push_back((Edge){to, from, dist});
        int m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }
    int ans = inf;
    REP(i, 1, N) {
        int tans = 0;
        Dijksta(i);
        REP(i, 1, N)
            tans += d[i] * cnt[i];
        ans = min(ans, tans);
    }
    cout << ans << endl;
    return 0;
}

本文转自:http://ljf-cnyali.cn/index.php/archives/143

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值