返回顶部

最大生成树+map实现技巧

POJ2263

  1 //#include<bits/stdc++.h>
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<vector>
  6 #include<cstring>
  7 #include<map>
  8 #include<set>
  9 #include<queue>
 10 #include<bitset>
 11 #include<utility>
 12 #include<functional>
 13 #include<iomanip>
 14 #include<sstream>
 15 #include<ctime>
 16 #include<cassert>
 17 #define fi first
 18 #define se second
 19 #define mp make_pair
 20 #define pb push_back
 21 #define pw(x) (1ll << (x))
 22 #define sz(x) ((int)(x).size())
 23 #define all(x) (x).begin(),(x).end()
 24 #define rep(i,l,r) for(int i=(l);i<(r);i++)
 25 #define per(i,r,l) for(int i=(r);i>=(l);i--)
 26 #define FOR(i,l,r) for(int i=(l);i<=(r);i++)
 27 #define debug1(a)       cout << #a << " = " << a << endl;
 28 #define debug2(a,b)     cout << #a << " = " << a << endl;\
 29     cout << #b << " = " << b << endl;
 30 #define debug3(a,b,c)   cout << #a << " = " << a << endl;\
 31     cout << #b << " = " << b << endl;\
 32     cout << #c << " = " << c << endl;
 33 #define debug4(a,b,c,d)\
 34     cout << #a << " = " << a << endl;\
 35     cout << #b << " = " << b << endl;\
 36     cout << #c << " = " << c << endl;\
 37     cout << #d << " = " << d << endl;
 38 #define eps 1e-9
 39 #define PIE acos(-1)
 40 #define cl(a,b) memset(a,b,sizeof(a))
 41 #define fastio ios::sync_with_stdio(false);cin.tie(0);
 42 #define lson l , mid , ls
 43 #define rson mid + 1 , r , rs
 44 #define ls (rt<<1)
 45 #define rs (ls|1)
 46 #define INF 0x3f3f3f3f
 47 #define lowbit(x) (x&(-x))
 48 #define sqr(a) a*a
 49 #pragma GCC optimize(2)
 50 using namespace std;
 51 typedef double db;
 52 typedef long long ll;
 53 typedef vector<int> vi;
 54 typedef pair<int, int> pii;
 55 
 56 
 57 
 58 const int maxn=2e5;
 59 int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn];
 60 int n,m,cnt;
 61 map<string,int>MAP;
 62 
 63 
 64 int cmp(const int i,const int j){return w[i]>w[j];}
 65 int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
 66 int kruscal(int t1,int t2)
 67 {
 68     int ans=0;
 69     rep(i,0,maxn)p[i]=r[i]=i;
 70     sort(r,r+m,cmp);
 71     rep(i,0,m){
 72         int e=r[i];
 73         int x=find(u[e]);
 74         int y=find(v[e]);
 75         if(x!=y){
 76             p[x]=y;
 77             if(find(t1)==find(t2)){
 78                 ans=w[e];
 79                 return ans;
 80             }
 81         }
 82     }
 83     return w[r[m]];
 84 }
 85 void Init()
 86 {
 87     MAP.clear();
 88     cnt=0;
 89 }
 90 int main()
 91 {
 92     int cas=0;
 93     while(cin>>n>>m,n&&m){
 94         Init();
 95         char s1[50],s2[50];
 96         rep(i,0,m){
 97             scanf("%s%s%d",s1,s2,&w[i]);
 98             if(!MAP.count(s1))MAP[s1]=cnt++;
 99             if(!MAP.count(s2))MAP[s2]=cnt++;
100             u[i]=MAP[s1];
101             v[i]=MAP[s2];
102 //            debug3(MAP[s1],MAP[s2],w[i]);
103         }
104         scanf("%s%s",s1,s2);
105         printf("Scenario #%d\n", ++cas);
106         printf("%d tons\n\n", kruscal(MAP[s1],MAP[s2]));
107 //        printf("Scenario #%d\n%d tons\n\n",++cas,kruscal(MAP[s1],MAP[s2]));
108     }
109     return 0;
110 }
View Code

 

posted @ 2018-09-13 14:30  牛奶加咖啡~  阅读(305)  评论(0编辑  收藏  举报