码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4781 Assignment For Princess(YY乱搞)

时间:2014-10-06 23:25:11      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   

http://acm.hdu.edu.cn/showproblem.php?pid=4781

Assignment For Princess
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 639    Accepted Submission(s): 196
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
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
 


题意:

要求构造一张n个点m条有向边的图,满足如下条件:

每对点间最多有一条边;

没有自环;

从任意一点出发,可以到达其他所有点;

m条边的权值为1,2,3,...,m,所有边的权值都不同;

从任意一点出发,最后要回到该点;

所有回路的权值和为3的倍数。

分析:

随便YY下就行了。

先构造1->2->3->4->...->n->1的环,边权依次为1,2,3,4,...,n;然后调整权值为n的边(当然也可已调整其他的边,这里只是为了方便),使得该环的权值和为3的倍数。然后按模3的余数对于剩下的边权分类,对于任意一条边权x,链接u,v两点,满足u->v路径上的边权和y与x对于3同余,比如1->2->3这条路径上的权值和为1+2=3,那么就可以取一条边权为99的边连1->3。


/*
 *
 * Author : fcbruce
 *
 * Time : Mon 06 Oct 2014 05:31:20 PM CST
 *
 */
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10

#ifdef _WIN32
  #define lld "%I64d"
#else
  #define lld "%lld"
#endif

#define maxm 88*88
#define maxn 88 

using namespace std;

struct _edge
{
  int u,v,w;
}edge[maxm];

int a[maxn<<1],s[maxm<<1];
bool vis[maxm];
bool G[maxn][maxn];

int main()
{
#ifdef FCBRUCE
  freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE

  int T_T,__=0;
  scanf("%d",&T_T);

  while (T_T--)
  {
    int n,m,cnt=0,last;
    memset(G,0,sizeof G);
    printf("Case #%d:\n",++__);
    scanf("%d%d",&n,&m);
    stack<int> r[3];
    a[0]=0;
    s[0]=0;
    for (int i=1;i<=n;i++)
    {
      a[i]=i;
      s[i]=s[i-1]+a[i];
    }

    memset(vis,0,sizeof vis);

    if (s[n]%3==0)
    {
      last=n;
    }
    else
    {
      if (s[n]%3==1){last=n+2;s[n]+=2;}
      else {last=n+1;s[n]+=1;}
    }

    vis[last]=true;

    for (int i=1;i<=n;i++)
    {
      a[i+n]=a[i];
      s[i]=s[i-1]+a[i];
    }

    for (int i=1;i<n;i++)
    {
      G[i][i+1]=G[i+1][i]=true;
      edge[cnt++]=(_edge){i,i+1,i};
      vis[i]=true;
    }

    edge[cnt++]=(_edge){n,1,last};
    G[n][1]=G[1][n]=true;

    for (int i=1;i<=m;i++)
    {
      if (vis[i]) continue;
      r[i%3].push(i);
    }

    for (int i=1;i<n;i++)
    {
      for (int k=0;k<n-2;k++)
      {
        int j=i+2+k;
        if (j>n) j-=n;
        if (G[i][j] || G[j][i]) continue;
        j=i+2+k;
        int re=s[j-1]-s[i-1];
        if (j>n) j-=n;
        re%=3;
        if (!r[re].empty())
        {
          G[i][j]=G[j][i]=true;
          edge[cnt++]=(_edge){i,j,r[re].top()};
          r[re].pop();
        }
        if (cnt==m) goto ok;
      }
    }
ok:;
   if (cnt<m) puts("-1");
   else
   {
    for (int i=0;i<cnt;i++)
      printf("%d %d %d\n",edge[i].u,edge[i].v,edge[i].w);
   }
      
  }


  return 0;
}


HDU 4781 Assignment For Princess(YY乱搞)

标签:acm   algorithm   

原文地址:http://blog.csdn.net/u012965890/article/details/39831163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!