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

本文介绍了一种构造特殊有向图的方法,确保任意两点间最多只有一条边且不存在自环,所有圈的权值之和均为3的倍数。通过构建初始环形路径并逐步添加剩余边来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
### HDU OJ 2610 2611 题目差异对比 #### 题目背景与描述 HDU OJ 平台上的第2610题第2611题均属于算法挑战类题目,旨在测试参赛者的编程能力逻辑思维能力。然而两道题目在具体的要求、输入输出格式以及解法复杂度方面存在显著不同。 对于第2610题《Bone Collector》,这是一个经典的背包问题变种案例[^1]。题目设定为收集骨头,在给定容量下最大化所获得的价。该问题通常通过动态规划方法求解,时间复杂度相对较低,适合初学者理解掌握基础的优化技巧。 而第2611题《Pick Apples》则涉及到更复杂的图论概念——最短路径寻找。在这个场景中,参与者扮演的角色需要在一个由节点组成的果园地图内移动来采摘苹果,并返回起点位置使得摘得果实数量最多的同时行走距离最小化。此类问题往往借助Dijkstra或Floyd-Warshall等经典算法实现高效处理方案的设计[^2]。 #### 输入输出样例分析 - **2610 Bone Collector** 输入部分提供了若干组数据集,每组包含两个整数n(物品数目)v(背包体积),随后给出各物品的具体重量wi及其对应价vi。最终程序需输出能够装载的最大价。 输出仅限于单个数表示最佳解决方案下的最高得分情况。 - **2611 Pick Apples** 此处不仅涉及到了边(即两点间所需消耗的时间/路程),还增加了顶点属性(如某棵树上挂有的果子量)。因此除了常规的邻接矩阵外还需要额外记录这些特殊参数用于辅助计算过程。最后的结果应呈现一条完整的路线列表连同累计收获了多少颗水果的信息一起展示出来。 综上所述,尽管两者都围绕着资源分配展开讨论,但从实际操作层面来看却有着本质区别:前者聚焦于单一维度内的最优组合选取;后者则是多因素综合考量下的全局最优策略制定。 ```cpp // 示例代码片段 - 动态规划解决2610 Bone Collector #include <iostream> using namespace std; int main() { int n, v; cin >> n >> v; vector<int> w(n), val(n); for (auto& i : w) cin >> i; for (auto& j : val) cin >> j; // dp数组初始化... } // 示例代码片段 - 图论算法应用于2611 Pick Apples #include <queue> #define INF 0x3f3f3f3f typedef pair<int,int> PII; vector<vector<PII>> adjList(N); // 存储加有向图 priority_queue<PII,vector<PII>,greater<PII>> pq; bool vis[N]; memset(vis,false,sizeof(vis)); while(!pq.empty()){ auto [dist,node]=pq.top(); pq.pop(); if(vis[node]) continue; ... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值