[SSL] 1693 & [Logu] P1828 Sweet butter

[SSL] 1693 & [Logu] P1828 Sweet butter

Time Limit:1000MS
Memory Limit:65536K

Description

Farmer John discovered the way to make the sweetest butter in Wisconsin: sugar. Putting the sugar on a pasture, he knew that N (1<=N<=500) cows would come and lick it, so that he could make super sweet butter that could sell for a good price. Of course, he will pay extra for the cows.
  Farmer John is very cunning. Like Pavlov before, he knew he could train these cows to go to a specific pasture when they heard the bell. He intends to put the sugar there and ring the bell in the afternoon so that he can express milk at night.
  Farmer John knows that every cow is on his favorite pasture (a pasture does not necessarily have only one cow). Give the route between the pasture and pasture where each cow is, find out the distance and shortest pasture for all the cattle to reach (he will put the sugar there)

Input

The first line: three numbers: the number of cows N, the number of pastures (2<=P<=800), the number of roads between pastures C (1<=C<=1450), the
second line to the N+1 line: 1 to The pasture number where the N cows are located.
Line N+2 to line N+C+1: Each line has three numbers: connected pastures A and B, and the distance between the two pastures D (1<=D<=255), of course , The connection is bidirectional

Output

One line outputs the minimum distance that the cow must walk and

Sample Input

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

Sample graphics

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

Sample Output

8 

{Description: It is best to put on the No. 4 ranch}

Ideas

Do the shortest path for single source p times, that is, the shortest path for multiple sources.
Calculate the distance sum.
Find the minimum value.

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct jgt
{
    
    
	int w,s;
};
struct NOTE
{
    
    
	int x,y,s,nxt;
}f[4000010];
bool operator < (const jgt &a,const jgt &b)
{
    
    
	return a.s>b.s;
}
priority_queue<jgt>q;
int n,p,c,dx,dy,tot=0;
int head[2010],cow[2010];
int dis[2010][2010];
bool d[2010];
void input()
{
    
    
	int i,j,x,y,z;
	scanf("%d%d%d",&n,&p,&c);
	for(i=1;i<=n;i++)
		scanf("%d",&cow[i]);
	for(i=1;i<=c;i++)
	{
    
    
		scanf("%d%d%d",&x,&y,&z);
		tot++;//建边 
		f[tot].x=x;
		f[tot].y=y;
		f[tot].s=z;
		f[tot].nxt=head[x];
		head[x]=tot;
		tot++;
		f[tot].x=y;
		f[tot].y=x;
		f[tot].s=z;
		f[tot].nxt=head[y];
		head[y]=tot;
	}
	return;
}
void DIJ(int x)
{
    
    
	jgt tem,temp;
	int i,j,mn,w,v;
	memset(d,0,sizeof(d));
	for(i=0;i<=p;i++)
		dis[x][i]=dis[i][x]=100000000;
	for(i=head[x];i>0;i=f[i].nxt)
	{
    
    
		dis[x][f[i].y]=dis[f[i].y][x]=f[i].s;
		tem.w=f[i].y;
		tem.s=f[i].s;
		q.push(tem);
	}
	for(dis[x][x]=0,d[x]=1;!q.empty();)//用小顶堆 
	{
    
    
		tem=q.top();
		q.pop();
		d[tem.w]=1;
		for(j=head[tem.w];j>0;j=f[j].nxt)//松弛 
		{
    
    
			if(!d[f[j].y]&&tem.s+f[j].s<dis[x][f[j].y])
			{
    
    
				dis[x][f[j].y]=dis[f[j].y][x]=tem.s+f[j].s;
				temp.w=f[j].y;
				temp.s=dis[f[j].y][x];
				q.push(temp);
			}
		}
	}
	return;
}
int main()
{
    
    
	int i,j,sum,ans=100000000;
	input();
	for(i=1;i<=p;i++)
	{
    
    
		DIJ(i);
		for(sum=0,j=1;j<=n;j++)//计算距离和 
			sum+=dis[i][cow[j]];
		ans=min(ans,sum);
	}
	printf("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/112135321