首页 > 代码库 > hdu 4781 Assignment For Princess(构造法)
hdu 4781 Assignment For Princess(构造法)
Assignment For Princess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 958 Accepted Submission(s): 286
Special Judge
Problem Description
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.
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 <= N2/7 )
For each test case, there is one line containing two integers N, M described above.(10 <= N <= 80, N+3 <= M <= N2/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.
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 8HintThe 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条边的有向图,每条边的权值分别为1,2,3........m,让你构
造满足下列条件的有向图。
1:每两个点之间最多只有一条有向边,且不存在自环。
2:从任意点出发都可以达到其他任意一个点,包括自己。
3:任意一个有向环的权值和都是3的倍数。
思路: 首先我们可以将点1到n连成一条链,边的权值分别是1到n-1,然后点n到点1连
一条边,若n%3为0或2,则边权值为n,否则边权值为n+2(m>=n+3),现在我们构造
出了一个环且满足上述三个条件。那么接下来如何构造剩下的m-n条边呢?
现在我们不管怎么构造都满足第二个条件了,而且现在每个点到自己的距离都是3的倍
数。那么如果我要在u,v两点之间连一条全值为len的边,那么只要满足len%3==dist[u][v]%3即
可(dist表示原环中两个点之间的距离),然后在构造的时候还要注意不要违背第一个条件,所
以我们可以用G[i][j]来表示i,j之间是否右边,如果按这样构造无法构造出图,则无解。
如图 : 要在点2与点4之间加一条权值为 len 的有向边 ,因为
( dist[2][4]+dist[4][2] ) % 3 = 0 , 加的权值为 len 的边要满足
(dist[4][2] + len)% 3 = 0 。有上述两式说明加的边的权值
只要满足len % 3==dist[2][4]%3 (因为是有向边,所以dist[2][4]!=dist[4][2] )。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int inf=999999999; const int M=7000; const int maxn=95; struct node { int u,v,val; node() {} node(int _u,int _v,int _val):u(_u),v(_v),val(_val) {} } a[M]; int dis[maxn][maxn],cnt,n,m; bool G[maxn][maxn],visited[M]; void initial() { cnt=0; memset(G,0,sizeof(G)); memset(visited,0,sizeof(visited)); for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) { if(i==j) dis[i][j]=0; else dis[i][j]=inf; } } void input() { scanf("%d %d",&n,&m); } void ready() { int t; for(int i=1; i<n; i++) { a[cnt++]=node(i,i+1,i); dis[i][i+1]=i; G[i][i+1]=1; visited[i]=1; } if(n%3==1) t=n+2; else t=n; a[cnt++]=node(n,1,t); dis[n][1]=t; visited[t]=1; G[n][1]=1; } void floyd() { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } bool judge(int len) { int tmp=len%3; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { if(i!=j && !G[i][j] && !G[j][i]) { if(dis[i][j]%3==tmp) { a[cnt++]=node(i,j,len); visited[len]=1; G[i][j]=1; return true; } } } return false; } void solve(int co) { floyd(); printf("Case #%d:\n",co); for(int i=1; i<=m; i++) if(!visited[i]) if(!judge(i)) { printf("-1\n"); return ; } for(int i=0;i<cnt;i++) printf("%d %d %d\n",a[i].u,a[i].v,a[i].val); } int main() { int T; scanf("%d",&T); for(int co=1; co<=T; co++) { initial(); input(); ready(); solve(co); } return 0; }
hdu 4781 Assignment For Princess(构造法)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。