• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
CodeForces 707B Bakery (水题,暴力,贪心)

题意:给定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;
}

  

posted on 2016-08-21 17:12  dwtfukgv  阅读(307)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3