zoukankan      html  css  js  c++  java
  • 洛谷——P1828 香甜的黄油 Sweet Butter

    香甜的黄油 Sweet Butter

    题目描述

    农夫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
    输出样例#1:
    8

    说明

    {样例图形

              P2  
    P1 @--1--@ C1
             |
             | 
           5  7  3
             |   
             |     C3
           C2 @--5--@
              P3    P4

    } {说明:

    放在4号牧场最优

    }

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 using namespace std;
     6 
     7 const long MAXN=9999;
     8 const long maxn=0x7fffffff;
     9 
    10 bool visit[MAXN];
    11 long MIN=maxn;
    12 long dis[MAXN],N,P,C,u,v,len,mc;
    13 long head[MAXN],num_edge,mcs[MAXN],temp;
    14 queue<long>que;
    15 
    16 struct Edge{
    17     long pre;
    18     long to;
    19     long len;
    20 }edge[MAXN];
    21 
    22 void in()
    23 {
    24     num_edge=0;
    25     for(long i=1;i<=C;i++){
    26         cin>>u>>v>>len;
    27         edge[++num_edge].pre=head[u];
    28         edge[num_edge].to=v;
    29         edge[num_edge].len=len;
    30         head[u]=num_edge;
    31         swap(u,v);
    32         edge[++num_edge].pre=head[u];
    33         edge[num_edge].to=v;
    34         edge[num_edge].len=len;
    35         head[u]=num_edge;
    36     }
    37 }
    38 
    39 void spfa(int a){
    40     que.push(a);
    41     visit[a]=true;
    42     while(!que.empty()){    
    43         long k=que.front();
    44         for(long j=head[k];j!=-1;j=edge[j].pre){
    45             long w=edge[j].len;
    46             if(dis[edge[j].to]>dis[k]+w){
    47                 dis[edge[j].to]=dis[k]+w;
    48                 if(!visit[edge[j].to]){
    49                     visit[edge[j].to]=true;
    50                     que.push(edge[j].to);
    51                 }
    52             }
    53         }
    54         visit[k]=false;    
    55         que.pop();
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     memset(visit,false,sizeof(visit));
    62     memset(head,-1,sizeof(head));
    63     
    64     while(!que.empty())
    65     que.pop();
    66     cin>>N>>P>>C;
    67     for(int i=1;i<=N;i++){
    68           cin>>mc;
    69           ++mcs[mc];
    70     }
    71     in();
    72     for(int i=1;i<=P;i++)
    73     {
    74         fill(dis,dis+MAXN,maxn);
    75         temp=0;
    76         dis[i]=0;
    77         spfa(i);
    78         for(int i=1;i<=P;i++)
    79             temp+=dis[i]*mcs[i];
    80         if(temp<MIN)
    81         MIN=temp;
    82     }
    83     cout<<MIN<<endl;
    84     return 0;
    85 }
  • 相关阅读:
    C++笔记(1):使用STL中sort()对struct排序
    Reading papers_13(gesture recognition survey,ing...)
    PCA算法学习_2(PCA理论的matlab实现)
    一些知识点的初步理解_8(Graph Cuts,ing...)
    基础学习笔记之opencv(18):kmeans函数使用实例
    opencv源码解析之(7):CvAdaptiveSkinDetectorl类
    Kinect+OpenNI学习笔记之11(OpenNI驱动kinect手势相关的类的设计)
    基础学习笔记之opencv(22):learning OpenCV书中一个连通域处理函数
    Reading papers_15(Graph cuts optimization for multilimb human segmentation in depth maps)
    Kinect+OpenNI学习笔记之12(简单手势所表示的数字的识别)
  • 原文地址:https://www.cnblogs.com/wsdestdq/p/6698847.html
Copyright © 2011-2022 走看看