题意:给定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 (水题,暴力,贪心)的更多相关文章

  1. hdu 1051:Wooden Sticks(水题,贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  3. CodeForces 705A Hulk (水题)

    题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...

  4. codeforces 707B B. Bakery(水题)

    题目链接: B. Bakery 题意: 是否存在一条连接特殊和不特殊的边,存在最小值是多少; 思路: 扫一遍所有边: AC代码: #include <iostream> #include ...

  5. 【Codeforces 707B】Bakery 水题

    对每个storages找一下最短的相邻边 #include <cstdio> #define N 100005 #define inf 0x3f3f3f3f using namespace ...

  6. Codeforces Round #368 (Div. 2) B. Bakery 水题

    B. Bakery 题目连接: http://www.codeforces.com/contest/707/problem/B Description Masha wants to open her ...

  7. CodeForces 705B (训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...

  8. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

  9. UVaLive 6698 Sightseeing Bus Drivers (水题,贪心)

    题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: ...

随机推荐

  1. setBackgroundDrawable和setBackgroundColor的用法

        1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getResources().getDrawable(R.drawa ...

  2. 最大流算法(Edmons-Karp + Dinic 比较) + Ford-Fulkson 简要证明

    Ford-Fulkson用EK实现:483ms #include <cstdio> #include <cstring> #define min(x,y) (x>y?y: ...

  3. UVa 11526 H(n)

    题意: long long H(int n){ long long res = 0; for( int i = 1; i <= n; i=i+1 ){ res = (res + n/i); } ...

  4. numa对MySQL多实例性能影响

     numa对MySQL多实例性能影响,通过对numa将MySQL绑定在不同的CPU节点上,并且采用绑定的内存分配策略,强制在本节点内分配内存.具体测试如下:1.关闭numa(numa= interle ...

  5. asp.net读excle的数据类型不统一取出空值问题

    如果表格里某列全是数字或是字符没问题,但如果混合了全是数字和部分字符就会有部分读取为空连接EXCEL方式如下 string strConn = "Provider=Microsoft.Jet ...

  6. MVC&WebForm对照学习:ajax异步请求

    写在前面:由于工作需要,本人刚接触asp.net mvc,虽然webform的项目干过几个.但是也不是很精通.抛开asp.net webform和asp.net mvc的各自优劣和诸多差异先不说.我认 ...

  7. 怎么制作生成苹果手机app应用的下载二维码图片

    原文网址:http://jingyan.baidu.com/article/8065f87ff654262331249886.html app store应用生成二维码操作步骤: 1.首先在MAC上的 ...

  8. 【转】有趣的Autolayout示例-Masonry实现

    原文网址:http://tutuge.me/2015/05/23/autolayout-example-with-masonry/ 好久没有写Blog了,这段时间有点忙啊=.=本文举了3个比较有“特点 ...

  9. Linux 中直接 I/O 机制的介绍

    https://www.ibm.com/developerworks/cn/linux/l-cn-directio/ 对于传统的操作系统来说,普通的 I/O 操作一般会被内核缓存,这种 I/O 被称作 ...

  10. 仿网易新闻客户端头条ViewPager嵌套实例

    要点: 1.重写组件public boolean onInterceptTouchEvent(MotionEvent event)方法 2.正确使用requestDisallowInterceptTo ...