码迷,mamicode.com
首页 > 其他好文 > 详细

2038 香甜的黄油

时间:2017-04-10 20:21:11      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:define   str   cow   int   esc   play   output   efi   display   

2038 香甜的黄油

USACO

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。

农夫John很狡猾。他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。

农夫John知道每只奶牛都在各自喜欢的牧场呆着(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

输入描述 Input Description

第一行: 三个数:奶牛数N,牧场数P(2<=P<=800),牧场间道路数C(1<=C<=1450).

第二行到第N+1行: 1到N头奶牛所在的牧场号.

第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距(1<=D<=255),当然,连接是双向的.

输出描述 Output Description

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

样例输入 Sample Input
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
样例图形
         P2  
P1 @--1--@ C1
    \    |     \   |       5  7  3
       \ |           \|    \ C3
      C2 @--5--@
         P3    P4
样例输出 Sample Output
8
{说明: 放在4号牧场最优. }
不好意思,最后一个点没过;或者可以找一下错误
 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<cstring>
 5 #define N 2010
 6 int map[N][N];
 7 int dis[N];
 8 int team[N],head,tail;
 9 int vis[N];
10 int cow[N];
11 int n,m,c,s; 
12 int minn=0x7fffffff;
13 void work(int i)
14 {
15     memset(team,0,sizeof(team));
16     memset(vis,0,sizeof(vis));
17     memset(dis,0x7f,sizeof(dis));
18     head=0;
19     tail=0;
20     team[tail++]=i;
21     dis[i]=0;
22     vis[i]=1;
23     while(head<tail)
24     {
25         int d=team[head];
26         for(int j=1;j<=n;++j)
27             {
28             if(map[d][j]!=0&&dis[j]>dis[d]+map[d][j])
29             {
30                 dis[j]=dis[d]+map[d][j];
31                 if(!vis[j])
32                 {
33                     team[++tail]=j;
34                     vis[j]=1;
35                 }
36             }
37         }
38         vis[d]=0;
39         head++;
40     }
41     s=0;
42     for(int k=1;k<=c;++k)
43     {
44         s+=(dis[cow[k]]);
45     }
46     if(s<minn&&s>=0)minn=s;
47 }
48 int main()
49 {
50     cin>>c>>n>>m;
51     int q,x,y;
52     for(int i=1;i<=c;++i)
53     {    
54         scanf("%d",&cow[i]);
55     }
56     for(int i=1;i<=m;++i)
57     {
58         scanf("%d%d%d",&x,&y,&q);
59         map[x][y]=q;
60         map[y][x]=q;
61     }
62     for(int i=1;i<=n;++i)
63     {
64         work(i);
65     }
66     cout<<minn;
67     return 0;
68 }

 



2038 香甜的黄油

标签:define   str   cow   int   esc   play   output   efi   display   

原文地址:http://www.cnblogs.com/mjtcn/p/6690656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!