CodeForces-707B(思维)

链接:

https://vjudge.net/problem/CodeForces-707B

题意:

Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.

To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.

Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.

Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).

Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.

思路:

选一个最小的边,同时两边的点满足一个是蛋糕店,一个是仓库.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 1e5+10;

struct Edge
{
    int u, v, w;
    bool operator < (const Edge& that) const
    {
        return this->w < that.w;
    }
}edge[MAXN];
int Vis[MAXN];
int n, m, k;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1;i <= m;i++)
        cin >> edge[i].u >> edge[i].v >> edge[i].w;
    int v;
    for (int i = 1;i <= k;i++)
    {
        cin >> v;
        Vis[v] = 1;
    }
    int res = -1;
    sort(edge+1, edge+1+m);
    for (int i = 1;i <= m;i++)
    {
        if (Vis[edge[i].u] != Vis[edge[i].v])
        {
            res = edge[i].w;
            break;
        }
    }
    cout << res << endl;

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11362147.html