【最小生成树】Codeforces 707B Bakery

题目链接:

  http://codeforces.com/problemset/problem/707/B

题目大意:

  给你N个点M条无向边,其中有K个面粉站,现在一个人要在不是面粉站的点上开店,问到面粉站的最短距离是多少。无法开店输出-1.

题目思路:

  【最小生成树】

  把边长按距离从小到大排序,出现的第一个只含一个面粉店的边为所求。

  

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 100004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 struct xxx
34 {
35     int b,e,d;
36 }a[N];
37 bool mark[N];
38 bool cmp(xxx aa,xxx bb)
39 {
40     return aa.d<bb.d;
41 }
42 int main()
43 {
44     #ifndef ONLINE_JUDGE
45 //    freopen("1.txt","r",stdin);
46 //    freopen("2.txt","w",stdout);
47     #endif
48     int i,j,k;
49     int x,y,z;
50 //    for(scanf("%d",&cas);cas;cas--)
51 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
52 //    while(~scanf("%s",s+1))
53     while(~scanf("%d",&n))
54     {
55         mem(mark,0);
56         scanf("%d%d",&m,&k);
57         for(i=1;i<=m;i++)
58             scanf("%d%d%d",&a[i].b,&a[i].e,&a[i].d);
59         sort(a+1,a+1+m,cmp);
60         for(i=1;i<=k;i++)
61         {
62             scanf("%d",&x);
63             mark[x]=1;
64         }
65         for(i=1;i<=m;i++)
66             if(mark[a[i].b]+mark[a[i].e]==1)break;
67         if(i>m)puts("-1");
68         else printf("%d\n",a[i].d);
69     }
70     return 0;
71 }
72 /*
73 //
74
75 //
76 */

时间: 2024-07-30 21:52:26

【最小生成树】Codeforces 707B Bakery的相关文章

Codeforces 707B. Bakery

题目链接:http://codeforces.com/problemset/problem/707/B 题意: 给你一个含有 n 个点, m 条边的无向带权图,以及 k 个点, 这 n 个点代表着 n 个城市, 边和权值代表着两个城市之间的路以及距离, k 个点代表着 n 个城市中有 k 个城市有面包店, 某人站在没有面包店的某个城市, 问你他到具有面包店的城市的最短距离. 思路: 把这 m 条边按照权值从小到大排序,然后依次检查每条边的两个端点,如果满足其中一个具有面包点,且其另外一个不具有,

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

题意:给定n个城市,其中有k个有仓库,问你在其他n-k个城市离仓库的最短距离是多少. 析:很容易想到暴力,并且要想最短,那么肯定是某一个仓库和某一个城市直接相连,这才是最优,所以只要枚举仓库,找第一个城市,然后更新答案即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib&g

codeforces 707B B. Bakery(水题)

题目链接: B. Bakery 题意: 是否存在一条连接特殊和不特殊的边,存在最小值是多少; 思路: 扫一遍所有边: AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include &l

【Codeforces 707B】Bakery 水题

对每个storages找一下最短的相邻边 #include <cstdio> #define N 100005 #define inf 0x3f3f3f3f using namespace std; struct edge{ int to,next,w; }e[N<<1]; int head[N],cnt; void add(int u,int v,int w){ e[++cnt]=(edge){v,head[u],w}; head[u]=cnt; } int n,m,k; int

CodeForces 76A Gift - 最小生成树

The kingdom of Olympia consists of N cities and M bidirectional roads. Each road connects exactly two cities and two cities can be connected with more than one road. Also it possible that some roads connect city with itself making a loop. All roads a

Codeforces Gym 100203H Highways 最小生成树

原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让你生成一颗最小生成树.模板题.图中已经有了的边要将权值置0.代码是队友写的. 代码 #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <

Codeforces Round #426 (Div. 2) D. The Bakery(线段树维护dp)

题目链接: Codeforces Round #426 (Div. 2) D. The Bakery 题意: 给你n个数,划分为k段,每段的价值为这一段不同的数的个数,问如何划分,使得价值最大. 题解: 考虑dp[i][j]表示划分为前j个数划分为i段的最大价值,那么这就是一个n*n*k的dp, 考虑转移方程dp[i][j]=max{dp[i][k]+val[k+1][j]},我们用线段树去维护这个max,线段树上每个节点维护的值是dp[i][k]+val[k+1][j],对于每加进来的一个数a

CodeForces 141E: ...(最小生成树)

[条件转换] 两两之间有且只有一条简单路径<==>树 题意:一个图中有两种边,求一棵生成树,使得这棵树中的两种边数量相等. 思路: 可以证明,当边的权是0或1时,可以生成最小生成树到最大生成树之间的任意值的生成树. 那么,方法就是生成最小生成树,然后,尽量替换0边,使得其成为值为(n-1)/2的生成树. 代码: 写的很乱,没有条理.还是应当先写出流程伪码后再敲代码的. #include <cstdio> #include <cstring> #include <v

Codeforces 834D The Bakery - 动态规划 - 线段树

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid