hdu 4781 Assignment For Princess(構造法),hduassignment
Assignment For PrincessTime 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.
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 )
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
16 8
Sample Output
Case #1:1 2 12 3 22 4 33 4 44 5 55 6 75 1 66 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;}