香甜的黄油-NC22947(Dijsktra法+枚举)

香甜的黄油

 

题号:NC22947
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。 
农夫John很狡猾。像以前的Pavlov,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。 
农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那) 

输入描述:

第一行: 三个数:奶牛数N,牧场数(2<=P<=800),牧场间道路数C(1<=C<=1450)
第二行到第N+1行: 1到N头奶牛所在的牧场号
第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距离D(1<=D<=255),当然,连接是双向的

输出描述:

一行 输出奶牛必须行走的最小的距离和

示例1

输入

复制

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

输出

复制

8

说明

 

This diagram shows the connections geometrically:

Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5 units; cow 3 walks 0 units -- a total of 8.

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 

//dijsktra
//start
const int MAX= 10020;
struct node {
    int x,to,next,w;
    bool operator <(const node &a) const{
        return this->w>a.w;
    }
}G[MAX<<1];
int cnt;
int dis[MAX];
int vis[MAX];
int head[MAX];


int n,p,c;
int b[10020];


void add(int u,int v,int w){
    G[++cnt].to=v;
    G[cnt].w=w;
    G[cnt].next=head[u];
    head[u]=cnt;
}
int  dijkstra(int s){
    mms(dis,INF);
    dis[s]=0;
    priority_queue<node> p;
    node t;
    t.x=s;
    t.w=0;
    p.push(t);
    while (!p.empty()) {
        node u=p.top();
        p.pop();
        int v=u.x;
        if(dis[v]!=u.w) continue;
        for(int i=head[v];i;i=G[i].next){
            int to=G[i].to;
            if(dis[to]>dis[v]+G[i].w){
                dis[to]=dis[v]+G[i].w;
                t.x=to;
                t.w=dis[to];
                p.push(t);
            }

        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        cnt+=dis[b[i]];
    }
    return cnt;
}


int main(){
    cin>>n>>p>>c;
    int u,v,w;
    for(int i=1;i<=n;i++)
        cin>>b[i];
    for(int i=1;i<=c;i++){
        cin>>u>>v>>w;
        add(u,v,w);
        add(v,u,w);
    }
    int ans=10000000;
    int cnt=0;
    for(int i=1;i<=p;i++){
        ans=min(ans,dijkstra(i));
    }
    cout<<ans;
}
//end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值