码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 707B Bakery (水题,暴力,贪心)

时间:2016-08-21 18:28:27      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定n个城市,其中有k个有仓库,问你在其他n-k个城市离仓库的最短距离是多少。

析:很容易想到暴力,并且要想最短,那么肯定是某一个仓库和某一个城市直接相连,这才是最优,所以只要枚举仓库,找第一个城市,然后更新答案即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}

vector<int> G[maxn];
vector<int> w[maxn];

bool a[maxn];

int main(){
    int k;
    scanf("%d %d %d", &n, &m, &k);
    memset(a, 0, sizeof(a));
    for(int i = 0; i < m; ++i){
        int x, y, v;
        scanf("%d %d %d", &x, &y, &v);
        G[x].push_back(y);
        G[y].push_back(x);
        w[x].push_back(v);
        w[y].push_back(v);
    }
    for(int i = 0; i < k; ++i){
        int x;
        scanf("%d", &x);
        a[x] = true;
    }
    if(k == 0){  printf("-1\n"); return 0; }
    LL ans = LNF;
    for(int i = 1; i <= n; ++i){
        if(a[i]){
            for(int j = 0; j < G[i].size(); ++j){
                int u = G[i][j];
                if(!a[u])  ans = Min(ans, (LL)w[i][j]);
            }
        }
    }

    if(ans == LNF)  printf("-1\n");
    else  printf("%I64d\n", ans);

    return 0;
}

  

CodeForces 707B Bakery (水题,暴力,贪心)

标签:

原文地址:http://www.cnblogs.com/dwtfukgv/p/5793075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!