HDU 4781 Assignment For Princess(构造所有圈权值和为3的倍数的有向图)

87 篇文章 0 订阅
28 篇文章 0 订阅

Assignment For Princess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1413    Accepted Submission(s): 419
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 <= 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.
 

Source
 

Recommend
We have carefully selected several similar problems for you:   5906  5905  5904  5903  5902

题目大意:
    输入顶点数N,边数M,要求构造一个,任意两点之间最多只有一条边,没有自环的,所有圈的权值和都为3的权值和。

解题思路:
    题目有点长,题意也比较恶心,说了一大堆废话,比赛的时候把有向边看成无向边了_(:з」∠)_。导致感觉自己的思路绝对不会有问题但WA到了比赛结束。。。
    首先我们先从1到N,构造一条链,然后再连接N和1,选择能用的最小的边,这样就形成了一个初始圆环,然后对于剩下的每条边,枚举边的起点i和终点j,若i到j的权值和与这条边的权值和同余,那么我们就相当于建立了一个没有任何影响的捷径。如果一条边不能插入,则输出-1。

AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))

const int maxn=80+3;

struct Edge
{
    int to,cost;
    Edge(int t,int c):to(t),cost(c){}
};

int G[maxn][maxn];//图的邻接矩阵表示
int N,M;

int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        mem(G,-1);
        for(int i=1;i<N;++i)//构造初始环
            G[i][i+1]=i;
        int used;//标记1和N加上的边
        bool ok=true;
        if(N%3==1)//特别处理N与1的权值
        {
            G[N][1]=N+2;
            used=N+2;
        }
        else
        {
            G[N][1]=N;
            used=N;
        }
        for(int u=N;u<=M;++u)//加入剩下的边
            if(u!=used)
            {
                bool add=false;//加边成功标志
                for(int i=1;i<=N;++i)
                {
                    for(int j=1;j<=N;++j)
                    {
                        if(j>i&&G[i][j]==-1&&G[j][i]==-1&&u%3==(i+j-1)*(j-i)/2%3)//添加等价边
                        {
                            G[i][j]=u;
                            add=true;
                            break;
                        }
                        else if(j<i&&G[i][j]==-1&&G[j][i]==-1&&(u%3+(i+j-1)*(j-i)/2)%3==0)//添加经过N~1的等价边
                        {
                            G[i][j]=u;
                            add=true;
                            break;
                        }
                    }
                    if(add)
                        break;
                }
                if(!add)
                {
                    ok=false;
                    break;
                }
            }
        printf("Case #%d:\n",cas++);
        if(!ok)
            puts("-1");
        else
        {
            for(int i=1;i<=N;++i)
                for(int j=1;j<=N;++j)
                    if(G[i][j]!=-1)
                        printf("%d %d %d\n",i,j,G[i][j]);
        }
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值