【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 107071
  • 排名 - 231

最新评论

阅读排行榜

评论排行榜

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

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

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

格式
PROGRAM NAME: butter

INPUT FORMAT:
(file butter.in)

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

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

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

OUTPUT FORMAT:
(file butter.out)

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

SAMPLE INPUT
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

SAMPLE OUTPUT
8


{样例图形

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

}

{说明:放在4号牧场最优:c1走3,c2走5,c3走0,相加为8}

【参考程序】:

/*
ID: XIONGNA1
PROG: butter
LANG: C++
*/
#include
<iostream>
#include
<cstring>
using namespace std;
const int maxn=99999999;
int l[801][501],queue[801],dis[801],cow[801],cost[801][801];
bool v[801];
int n,p,c,ans;
void spfa(int s)
{
    
int head,tail,now,start;
    
for (int i=1;i<=p;i++)
    {
        dis[i]
=maxn; v[i]=false;
    }
    head
=1; tail=1;
    v[s]
=true; queue[1]=s; dis[s]=0;
    
while (head<=tail)
    {
        start
=queue[(head-1)%p+1];
        
for (int i=1;i<=l[start][0];i++)
        {
            now
=l[start][i];
            
if (dis[start]+cost[start][now]<dis[now])
            {
                dis[now]
=dis[start]+cost[start][now];
                
if (!v[now])
                {
                    v[now]
=true;
                    tail
++; queue[(tail-1)%p+1]=now;
                }
            }
        }
        v[start]
=false;
        head
++;
    }
    
int sum=0;
    
for (int i=1;i<=n;i++)
        sum
+=dis[cow[i]];
    
if (ans>sum) ans=sum;
}
int main()
{
    freopen(
"butter.in","r",stdin);
    freopen(
"butter.out","w",stdout);
    scanf(
"%d%d%d",&n,&p,&c);
    
for (int i=1;i<=p;i++)
        
for (int j=1;j<=p;j++)
            
if (i==j) cost[i][j]=0;
            
else cost[i][j]=maxn;
    
for (int i=1;i<=n;i++) scanf("%d",&cow[i]);
    
int x,y,s;
    memset(l,
0,sizeof(l));
    
for (int i=1;i<=c;i++)
    {
        scanf(
"%d%d%d",&x,&y,&s);
        cost[x][y]
=s; cost[y][x]=s;
        l[x][
0]++; l[x][l[x][0]]=y;
        l[y][
0]++; l[y][l[y][0]]=x;
    }
    ans
=maxn;
    
for (int x1=1;x1<=p;x1++) spfa(x1);
    printf(
"%d\n",ans);
    
return 0;
}
posted on 2009-07-25 09:24 开拓者 阅读(357) 评论(0)  编辑 收藏 引用 所属分类: USACO 题解

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理