dsu greedy implementation *1200

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
def valid(i,j,n,m):
        if i<n and i>=0 and j>=0 and j< m :return True         return  False
def sumn(i,n):
    return (n-i)*(i+n)/2
def sqfun(a,b,c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return 
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]
def words(): return [i for i in input().split()]


def f():
    dd={}
    i=0
    for w in sys.stdin.readline().split():
            dd[w]=i
            i+=1
            wd.append(w)
    return dd
def kin():
    
    l=[int(i)-1 for i in input().split()]
    nn=l.pop(0)
    l=sorted(l)
    mn=cost[l[0]]
    for i in l:
        mn=min(mn,cost[i])
        d[wd[i]]=l[0]
    cost[l[0]]=mn
def solve():
    global wd ,d,cost
    wd=[]
    n,k,m=values()
    d=f()
    cost=inlsts()
    for i in range(k):
        kin()
    tot=0
    r=words()
    for i in r:tot+=cost[d[i]]
    return tot





if __name__ == "__main__":
           print(solve()) 

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define en cout<<endl;
using namespace std;
void fast(){
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
//int fre[10000004]={0};

void solve(){
	int n,k,m1;
	cin>>n>>k>>m1;
	pair<string,int> arr[n];
	map<string,int>mp;
	for(int i=0;i<n;i++){
		cin>>arr[i].first;
	}
	for(int i=0;i<n;i++){
		cin>>arr[i].second;
		mp[arr[i].first]=arr[i].second;
	}
	for(int i=0;i<k;i++){
		int m;
		cin>>m;
		int ar[m],mn;
		for(int j=0;j<m;j++){
			cin>>ar[j];
			if(!j)mn=arr[ar[j]-1].second;
			else mn=min(mn,arr[ar[j]-1].second);
		}
		for(int j=0;j<m;j++){
			arr[ar[j]-1].second=min(mn,arr[ar[j]-1].second);
			mp[arr[ar[j]-1].first]=arr[ar[j]-1].second;
		}
	}
	ll ans=0;
	for(int i=0;i<m1;i++){
		string s;
		cin>>s;
		ans+=mp[s];
	}
	cout<<ans;
}

int main(){
    fast();
    int t=1;
//    cin>>t;
    while(t--){
        solve();
        en
    }
    
}
/*

4 2
4
1 2 3 4
4
1 2 3 4


*/


Comments

Submit
0 Comments
More Questions

74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations