Hdu 4781 Assignment For Princess (constructor), hduassignment

Source: Internet
Author: User

Hdu 4781 Assignment For Princess (constructor), 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 Away, there lived has 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 cocould graduate in time.
Or your wedding wowould 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 has ed 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 wocould probably be several solutions, but your project wocould 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 cocould 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 shoshould 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 8Hint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. 
 



There is a directed graph with n points and m edges. the weights of each edge are 1, 2, and 3 ...... m.

Create a directed graph that meets the following conditions.

1: Each two points can have at most one directed edge, and there is no self-ring.

2: You can start from any point to reach any other point, including yourself.

3: The sum of the weights of any directed ring is a multiple of 3.

Train of Thought: first, we can connect points 1 to n into a chain. The Edge weights are 1 to n-1, and then point n to 1.

An edge. If n % 3 is 0 or 2, the edge weight is n. Otherwise, the edge weight is n + 2 (m> = n + 3). Now we construct

A ring is generated and the preceding three conditions are met. Then how can we construct the remaining m-n edges?

Now we meet the second condition no matter how we construct it, and the distance from each point to ourselves is three times.

Number. If I want to connect an edge with a full value of len between u and v, if len % 3 = dist [u] [v] % 3 is met

Yes (dist indicates the distance between two points in the original ring), and pay attention not to violate the first condition during construction.

In this example, we can use G [I] [j] to indicate whether the right side is between I and j. If we cannot construct a plot using this structure, there is no solution.


: Add a directed edge with the weight of len between vertex 2 and vertex 4, because

(Dist [2] [4] + dist [4] [2]) % 3 = 0. The added edge with the weight of len must satisfy

(Dist [4] [2] + len) % 3 = 0. The two formula above indicates the edge weight.

As long as len % 3 = dist [2] [4] % 3 is satisfied (because there is a directed edge, 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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.