标签:
并不是很难的一个构造,我在比赛的时候把题目读错了,补题的时候想得比较粗糙,迟迟没过这题,之后想法慢慢细致起来,还是将这题过了。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1e9+10; int n,m; struct Edge { int u,v,w; friend bool operator<(Edge A,Edge B) { return A.w<B.w; } };Edge ans[maxn];int ansn; int flag; int cur; int e[120][120]; bool vis[120][120]; bool used[maxn]; void putA() { REP(i,1,n) REP(j,1,n) e[i][j]=i==j?0:INF; ansn=0; MS0(vis);MS0(used); int sum=0; int cur=4; int finish=0; REP(i,1,n-1){ if(cur>m){ finish=1; cur=3; } int j=i+1; e[i][j]=cur; vis[i][j]=vis[j][i]=1; used[cur]=1; ans[++ansn]={i,j,cur}; sum+=cur; if(finish) cur+=3; else if(i%2) cur++; else cur+=2; } REP(i,1,m){ if(used[i]) continue; if((sum+i)%3==0){ used[i]=1; vis[n][1]=vis[1][n]=1; e[n][1]=i; ans[++ansn]={n,1,i}; sum+=i; break; } } if(sum%3) flag=-1; } void floyd() { REP(k,1,n){ REP(i,1,n){ //if(k==i) continue; REP(j,1,n){ //if(j==k||j==i) continue; if(e[i][k]+e[k][j]<e[i][j]) e[i][j]=e[i][k]+e[k][j]; } } } } void putB() { //REP(i,1,n) if(e[i][i%n+1]==INF) cout<<"i="<<i<<" j="<<i%n+1<<" e="<<e[i][i%n+1]<<endl; floyd(); /* REP(i,1,n) REP(j,1,n){ if(e[i][j]==INF) cout<<"i="<<i<<"j="<<j<<" e="<<e[i][j]<<endl; } */ REP(i,1,m){ if(used[i]) continue; int tag=0; REP(j,1,n){ REP(k,1,n){ //if(i==6&&j!=k) cout<<e[j][k]<<" "<<j<<" "<<k<<" "<<vis[j][k]<<endl; if(j==k||vis[j][k]||e[j][k]%3!=i%3) continue; used[i]=1; vis[j][k]=vis[k][j]=1; ans[++ansn]={j,k,i}; tag=1;break; } if(tag) break; } } //REP(i,1,m) cout<<used[i]<<" ";cout<<endl; REP(i,1,m) if(!used[i]) flag=-1; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int T;cin>>T;int casen=1; while(T--){ scanf("%d%d",&n,&m); flag=0; putA(); if(~flag) putB(); printf("Case #%d:\n",casen++); if(flag==-1) puts("-1"); else{ sort(ans+1,ans+ansn+1); REP(i,1,ansn) printf("%d %d %d\n",ans[i].u,ans[i].v,ans[i].w); } } return 0; } /** 7 6 8 5 8 6 9 6 8 10 13 10 14 7 11 4 10 13 10 14 80 83 80 974 */
标签:
原文地址:http://www.cnblogs.com/--560/p/5259359.html