Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define ll long long
- #define pii pair<int,int>
- #define pll pair<ll,ll>
- //#define M 100007
- #define INF 1e9
- #define INFL 1e18
- #define PI acos(-1)
- #define mp make_pair
- #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #include<bits/stdc++.h>
- using namespace std;
- #define M 10000
- #define ll long long
- ll pr[M];
- struct edge
- {
- long long u,v,w;
- bool operator<(const edge& p)const
- {
- return w<p.w;
- }
- };
- ll par[10005];
- vector<edge>e;
- ll findp(ll v)
- {
- if(par[v]==v)
- return v;
- return par[v]=findp(par[v]);
- }
- void mst(ll n)
- {
- sort(e.begin(),e.end());
- for(ll i=0;i<=n;i++)
- {
- par[i]=i;
- }
- ll cnt=0;
- for(ll i=0;i<e.size();i++)
- {
- ll u,v;
- u=(findp(e[i].u));
- v=(findp(e[i].v));
- if(u!=v)
- {
- char a,b;
- a=e[i].u+'A';
- b=e[i].v+'A';
- if(a>b)
- swap(a,b);
- int c=e[i].w;
- cout<<a<<'-'<<b<<' '<<c<<endl;
- par[u]=v;
- cnt++;
- }
- if(cnt==n-1)
- break;
- }
- }
- int main()
- {
- ll t;
- cin>>t;
- for(ll i=1;i<=t;i++)
- {
- ll n,m=0;
- cin>>n;
- e.clear();
- for(ll j=0;j<n;j++)
- {
- for(ll k=0;k<n;k++)
- {
- ll w;
- cin>>w;
- getchar();
- if(k>j && w>0)
- {
- m++;
- edge get;
- get.u=j;
- get.v=k;
- get.w=w;
- e.pb(get);
- }
- }
- }
- cout<<"Case "<<i<<":"<<endl;
- mst(m);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment