2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 6 J. Sky City

J. Sky City

Title link: https://ac.nowcoder.com/acm/contest/9986/J

Title description:

There are 5 towns in Sky City, named Ada, Aed, Akk, Orz, Apq, and they also have mutual path lengths.

Cida has long been looking forward to the City of the Sky. Now that she has boarded the City of the Sky, she wants to go to every city in the City of the Sky, but she hopes that the length of the road she walks is as small as possible, so as to save energy and save money. time.

Baru agreed, but because he was the main force (boy), he needed to help Cedar calculate the shortest path length to travel through all the cities.

Due to the magical power of the Sky City, if Cida wants to take the road she has walked once again, she can spend no time on this road.

But there are too many cities in the Sky City, he can't calculate it, so he has to ask you to help.

Enter a description:

On the first line, enter n, q, which means there are n cities and q sides;

On the second line, enter a name tmp, indicating that Cida wants to start walking from tmp city;

Next line q, enter two names a, b and a number val in each line, indicating that the distance between city a and city b is val. (Note that there may be double edges and self-loops)

Output description:

Help Baru calculate the shortest path length. If it can't travel all the cities, output "No!".

Example 1:

Input
5 5
Orz
Ada Aed 5
Orz Ada 6
Apq Aed 8
Akk Apq 12
Aed Orz 3
Output
28
description
Ada->Aed->Orz->Aed->Apq->Akk
Remarks:

Multiple groups of input and output (end with EOF), ensure that the number of data groups does not exceed 10.

1 <= n <= 5000, 1 <= q <= 200000, 1 <= val <= 1e9. The length of the name of each city does not exceed 10.

Ensure that ∑q≤200000.

Problem-solving ideas:

Minimal spanning tree template question Use map to convert string to number (map <string, int> mp), and then it is the minimal spanning tree template question with n points and m edges.
Minimum spanning tree template code:
kruskal algorithm for
minimum spanning tree, prim algorithm for minimum spanning tree

code show as below:

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
#define ll long long
map <string, int> mp;
int n,m,cnt=0;
string s;
struct node{
    
    
	int x,y,p;
}a[200010];
int pre[10010];
bool cmp(node a,node b){
    
    
	return a.p<b.p;
}
int find(int x){
    
    
	if(x!=pre[x]){
    
    
		pre[x]=find(pre[x]);
	}
	return pre[x];
}
void merge(int x, int y){
    
    
	int fx=find(x),fy=find(y);
	if(fx!=fy) pre[fy]=fx;
}
int main(){
    
    
	while(scanf("%d%d",&n,&m)!=EOF){
    
    
		mp.clear();
		cnt=0;
		cin>>s;
		for (int i=1;i<=m;i++){
    
    
			string s1,s2;
			int x;
			cin>>s1>>s2>>x;
			if (mp.find(s1)==mp.end()) mp[s1]= ++cnt;
			if (mp.find(s2)==mp.end()) mp[s2]= ++cnt;
			a[i].x=mp[s1];
			a[i].y=mp[s2];
			a[i].p=x;
		}
		sort(a+1,a+1+m,cmp);
		for (int i=1;i<=n;i++)
			pre[i]=i;
		ll ans=0;
		int num=0;
		for (int i=1;i<=m;i++){
    
    
			if (find(a[i].x)!=find(a[i].y)){
    
    
				ans+=a[i].p;
				num++;
				merge(a[i].x,a[i].y);
			}
		}
		if (num<n-1) 
			printf("No!\n");
		else 
			printf("%lld\n",ans);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/114087630