【HDU - 4781 】Assignment For Princess (构造)

2 篇文章 0 订阅

  Long long ago, in the Kingdom Far Far Away, there lived many little animals. And you are the beloved princess who is marrying the prince of a rich neighboring kingdom. The prince, who turns out to be a handsome guy, offered you a golden engagement ring that can run computer programs! 
  The wedding will be held next summer because your father, the king, wants you to finish your university first. 
  But you did’t even have a clue on your graduation project. Your terrible project was to construct a map for your kingdom. Your mother, the queen, wanted to make sure that you could graduate in time. 
  Or your wedding would have to be delayed to the next winter. So she told you how your ancestors built the kingdom which is called the Roads Principle: 

  1. Your kingdom consists of N castles and M directed roads. 
  2. There is at most one road between a pair of castles. 
  3. There won’t be any roads that start at one castle and lead to the same one. 
  She hoped those may be helpful to your project. Then you asked your cousin Coach Pang (Yes, he is your troubling cousin, he always asks you to solve all kinds of problems even you are a princess.), the Minister of Traffic, about the castles and roads. Your cousin, sadly, doesn’t have a map of the kingdom. Though he said the technology isn’t well developed and it depends on your generation to contribute to the map, he told you the Travelers Guide, the way travelers describe the amazing road system: 
  1. No matter which castle you start with, you can arrive at any other castles. 
  2. Traveling on theM roads will take 1, 2, 3, ... ,M days respectively, no two roads need the same number of days. 
  3. You can take a round trip starting at any castle, visiting a sequence of castles, perhaps visiting some castles or traveling on some roads more than once, and finish your journey where you started. 
  4. The total amount of days spent on any round trip will be a multiple of three.
  But after a month, you still couldn’t make any progress. So your brother, the future king, asked your university to assign you a simpler project. And here comes the new requirements. Construct a map that satisfies both the Roads Principle and the Travelers Guide when N and M is given. 
  There would probably be several solutions, but your project would be accepted as long as it meets the two requirements. 
Now the task is much easier, furthermore your fiance sent two assistants to help you. 
  Perhaps they could finish it within 5 hours and you can think of your sweet wedding now.

Input

  The first line contains only one integer T, which indicates the number of test cases. 
  For each test case, there is one line containing two integers N, M described above.(10 <= N <= 80, N+3 <= M <= N 2/7 )

Output

  For each test case, first output a line “Case #x:”, where x is the case number (starting from 1). 
  Then output M lines for each test case. Each line contains three integers A, B, C separated by single space, which denotes a road from castle A to castle B and the road takes C days traveling. 
  Oh, one more thing about your project, remember to tell your mighty assistants that if they are certain that no map meets the requirements, print one line containing one integer -1 instead. 
  Note that you should not print any trailing spaces.

Sample Input

1
6 8

Sample Output

Case #1:
1 2 1
2 3 2
2 4 3
3 4 4
4 5 5
5 6 7
5 1 6
6 1 8

        
  

Hint

The restrictions like N >= 10 will be too big for a sample. So the sample is just a simple case for the detailed formats of input and output,
 and it may be helpful for a better understanding. Anyway it won’t appear in actual test cases.

题意;

有n个点m条边,一开时N个点之间没有连边,要求加入单向边(不能有重边),使得构造的图满足以下条件。

1、边权不能相同,且边权只能取1~m之间的数

2、强连通图

3、无自环

4、任意一条闭合回路的边权之和是3的倍数。

思路:

根据题目的要求构造,需要注意的是单向边,因此最后一条边是从n->1,而其他的边都是a->b(a<b),注意输出的时候注意,n->1这一条边不能输出反了。因为是单向边,又要构成环因此在i->j加一条直接相连的边的时候,要保证w%3==sum(i,j)%3,即加入的边的权值对3取余和从i到j的所有长度为1的边的边权之和对3取余的值是一样的,因此新加入的边可以取代刚才求和的边,来和其他边组成环。

构造思路,首先构造1->2,2->3...i->i+1....n-1->n,这n条边,然后从n,n+1,n+2这3条边中选一条合适的作为n->1的边。因为边的数量不大,因此枚举剩下的边,找两点可以加入这条边的地方。

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;

int dis[100][100];
set<int> ss;
set<int> ::iterator it; 
int main(){
	int t,cas=1;
	cin>>t;
	while(t--){
		ss.clear();
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=0;i<=n+5;i++)
		for(int j=0;j<=n+5;j++)
		dis[i][j]=-1;
		for(int i=1;i<=n-1;i++){
			dis[i][i+1]=i;
		} 
		int sum=(n-1)*n/2;
		sum%=3;
		int tm;
		if(n%3==(3-sum)%3){
			tm=n;ss.insert(n+1);ss.insert(n+2);
		}
		if((n+1)%3==(3-sum)%3){
			tm=n+1;ss.insert(n);ss.insert(n+2);
		}
		
		if((n+2)%3==(3-sum)%3){
			tm=n+2;ss.insert(n+1);ss.insert(n);
		}
		dis[n][1]=tm;//单向边,因此只能有这一条,输出1~n是不对的 
		for(int i=n+3;i<=m;i++){
			ss.insert(i);
		}
		int ff=1;
		for(it=ss.begin();it!=ss.end();it++){
			int fl=0;
			for(int i=2;i<n;i++){
				for(int j=1;j<=n;j++){
					int ed=j+i;
					if(ed>n) break;;
					if(dis[j][ed]!=-1) continue;
					if(((ed-j)*(ed+j-1)/2)%3==(*it)%3)
					{
						fl=1;dis[j][ed]=(*it);//dis[ed][j]=(*it);
						break;
					}
					
				} 
				if(fl) break;
			}
			if(fl==0){
				ff=0;break;
			}
			
			
		}
		printf("Case #%d:\n",cas++);
		if(!ff)
		printf("-1\n");
		else{
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(dis[i][j]!=-1)
					printf("%d %d %d\n",i,j,dis[i][j]);
				}
			}
		}
	} 
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值