Luogu P2935 最好的地方Best Spot

Luogu P2935 最好的地方Best Spot

这道题就是一道近乎裸的Floyd,因为数据很小,所以用领接表存图就可以了。

#include<bits/stdc++.h>
#define INF 0x3fffffff
#define N 510

using namespace std;

int p,f,c,mmin=INF,sum,ans;
int like[N],e[N][N];

void Read() {
    scanf("%d%d%d",&p,&f,&c);
    for(int i=1;i<=p;i++) {
        for(int j=1;j<=p;j++) {
            e[i][j]=INF;
        }
        e[i][i]=0;
    }
    for(int i=1;i<=f;i++) {
        scanf("%d",&like[i]);
    }
    for(int i=1;i<=c;i++) {
        int a,b,t;
        scanf("%d%d%d",&a,&b,&t);
        e[a][b]=t;
        e[b][a]=t;
    }
    return;
}

void Floyd() {
    for(int k=1;k<=p;k++) {
        for(int i=1;i<=p;i++) {
            for(int j=1;j<=p;j++) {
                e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
            }
        }
    }
    return;
}

void Print() {
    for(int i=1;i<=p;i++) {
        sum=0;
        for(int j=1;j<=f;j++) {
            sum+=e[i][like[j]];
        }
        if(sum<mmin) {
            mmin=sum;
            ans=i;
        }
    }
    printf("%d",ans);
    return;
}

int main()
{
    Read();
    Floyd();
    Print();
    return 0;
}

原文地址:https://www.cnblogs.com/luoshui-tianyi/p/11415429.html

时间: 2024-08-05 00:33:42

Luogu P2935 最好的地方Best Spot的相关文章

洛谷——P2935 [USACO09JAN]最好的地方Best Spot

P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures F_i of the P (1 <= P <= 500; 1 <= F_i <= P) total pastures (conveniently nu

洛谷 P2935 [USACO09JAN]最好的地方Best Spot

P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures F_i of the P (1 <= P <= 500; 1 <= F_i <= P) total pastures (conveniently nu

[USACO09JAN]最好的地方Best Spot

OJ题号:洛谷2935 思路:Floyd 1 #pragma GCC optimize ("O3") 2 #include<cstdio> 3 #include<cctype> 4 #define inf 0x7fffffff>>1 5 inline int min(const int a,const int b) { 6 return a<b?a:b; 7 } 8 inline int getint() { 9 int ch; 10 whil

luogu P3799 妖梦拼木棒

二次联通门 : luogu P3799 妖梦拼木棒 /* luogu P3799 妖梦拼木棒 用一个桶存下所有的木棒 美剧两根短的木棒长度 后随便乘一乘就 好了.. */ #include <algorithm> #include <cstdio> #define Mod 1000000007 #define Max 5000 void read (int &now) { now = 0; register char word = getchar (); while (wo

[luogu P1967][NOIp2013]P1967 货车运输

题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物. 输入输出格式 输入格式: 输入文件名为 truck.in. 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道 路. 接下来 m 行每行 3 个整数 x. y. z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z

luogu 3126 回文的路径

https://www.luogu.org/problem/show?pid=3126 考虑dp,从两头走到中间. f[i][j][k][l]表示从左上角走到(i,j),从右下角走到(k,l),路径长度相等,所经过路径相同的方案数. 方程不再赘述. 考虑步数要相同,所以只要枚举步数和行就好. f[i][j][k]表示第一个点在第j行,第2个点在第k行,走i步的方案数. 所以得出方程f[i][j][k]=(f[i-1][j-1][k]+f[i-1][j][k+1]+f[i-1][j-1][k+1]

luogu P2018 消息传递

二次联通门 : luogu P2018 消息传递 /* luogu P2018 消息传递 树形dp 原来用优先队列做了一下, T了俩点 MMP 去看正解.. 复杂度一样好不好.. 每次到达一个点,记录其子树中所有的dp值 优先向大的一边转移 */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define INF 1e8 const int BUF

luogu P1966 火柴排队

二次联通门 : luogu P1966 火柴排队 /* luogu P1966 火柴排队 神TM逆序对... noip怎么这么坑啊.. 暴力都没得打 此题模拟考试时爆了0 做法 将A数组排序,由于B数组与A数组是一一对应的 那么B数组的位置也会发生相应的变化 此时B数组逆序数对数即为答案 */ #include <cstdio> #include <iostream> #include <algorithm> const int BUF = 123123123; cha

luogu P1941 飞扬的小鸟

二次联通门 : luogu P1941 飞扬的小鸟 /* luogu P1941 飞扬的小鸟 dp 向上飞是完全背包,向下掉就是01背包 分情况讨论一下 最后合并一下 */ #include <cstdio> #include <iostream> #include <cstring> const int BUF = 123123123; char Buf[BUF], *buf = Buf; inline void read (int &now) { for (