您的位置:首页 > 其它

codevs 2038 香甜的黄油 SPFA 解题报告

2017-10-05 20:57 441 查看

题目描述 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号牧场最优. }

数据范围及提示 Data Size & Hint

见描述

思路

很简单的最短路,枚举一下就好了。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,p,c;
int dis[801],a[801][801],b[501],dl[805],t,w,tot,mn=0x7fffffff;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void spfa(int x)
{
memset(dis,127,sizeof(dis));
int t=0,w=1;
bool pd[801]={0};
dl[t]=x;
pd[x]=1;
dis[x]=0;
while(t!=w)
{
for(int i=1;i<=p;i++)
{
if(a[dl[t]][i]+dis[dl[t]]<dis[i])
{
dis[i]=a[dl[t]][i]+dis[dl[t]];
if(!pd[i])
{
dl[w]=i;
w++;
w=(w-1)%801+1;
pd[i]=1;
}
}
}
pd[dl[t]]=0;
t++;
t=(t-1)%801+1;
}
tot=0;
for(int i=1;i<=n;i++)
tot+=dis[b[i]];
}
int main()
{
memset(a,127,sizeof(a));
n=read();p=read();c=read();
int x,y,z;
for(int i=1;i<=n;i++)
b[i]=read();
for (int i=1;i<=c;i++)
{
x=read();y=read();z=read();
a[x][y]=a[y][x]=z;
}
for(int i=1;i<=p;i++)
{
spfa(i);
if (tot<mn) mn=tot;
}
cout<<mn;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: