日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 4781】Assignment For Princess(图上构造) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

  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條邊,你要構造出來一個有向圖,有向圖的邊權各不相同,從1到m 。并且要求從一個點到其他所有點都是聯通的,還要求從一個點出發,回到該點走的路徑權值一定要是3的倍數,并且任意兩點之間最多只能有一條邊,

解題報告:

因為題目保證有解,所以直接連接所有頂點成環,成鏈的邊的權值直接就是1~n-1就行,因為n號點到1號點的邊一定可以通過其他權值構造出來,因為這題保證了N+3 <= M。構造一個環了之后看還剩下多少權值,在模3相等的點上直接加邊就行了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e4 + 5; bool ok[MAX]; int sum[MAX]; int n,m,top[MAX],qq[3][MAX]; int main() {int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&m);memset(sum,0,sizeof sum);printf("Case #%d:\n",++iCase);for(int i = 1; i<=n-1; i++) {printf("%d %d %d\n",i,i+1,i);sum[i+1] = sum[i] + i;}top[0]=top[1]=top[2]=0;for(int val = n; val<=m; val++) qq[val%3][++top[val%3]] = val;int x = (30000000 + (sum[1]-sum[n])%3)%3;printf("%d 1 %d\n",n,qq[x][top[x]--]);for(int i = 1; i<=n-2; i++) {for(int j = i+2; j<=n; j++) {if(i!=1 || j!=n) {int x = (30000000 + sum[j] - sum[i])%3;if(top[x]) printf("%d %d %d\n",i,j,qq[x][top[x]--]);}}}}return 0 ; } /* 1 4 7*/

?

總結:

注意前綴和算的時候不能這樣寫:

for(int i = 1; i<n-1; i++) {printf("%d %d %d\n",i,i+1,i);sum[i] = sum[i-1] + i;ok[i]=1;sum += i;}

另一點:這兩句是等價的。當不好估計上界的時候就可以下面這樣寫。

int x = (30000000 + (sum[1]-sum[n])%3)%3; int x = (3+(sum[1]-sum[n])%3)%3;

?

總結

以上是生活随笔為你收集整理的【HDU - 4781】Assignment For Princess(图上构造)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。