Problem H. Hard Test
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100342/attachments

Description

Andrew is having a hard time preparing his 239-th contest for Petrozavodsk. This time the solution to the problem is based on Dijkstra algorithm and Andrew wants to prepare the hard test for the algorithm.
The Dijkstra algorithm is used to find the shortest path from a source vertex to all other vertices in a graph. The algorithm acts as follows. Let G be a weight directed graph with vertex set V , edge set E and weight function w : E → R +. Let all vertices be reachable from vertex s. The algorithm uses a set
of vertices U, first initialized as empty. Each vertex is labeled with either an integer number, or with +∞. Initially all vertices are labeled with +∞, and the vertex s is labeled with 0. Denote the label of vertex v as d[v].
A step of the algorithm is the following: the vertex with the minimal label that doesn’t belong to U is selected. Let this vertex be u. The vertex u is added to the set U, and each edge uv ∈ E is relaxed. The relaxation replaces d[v] with min(d[v], d[u] + w(uv)). The algorithm is over when all vertices belong to U. If the label of the vertex v has changed, the relaxation is said to be active.
Now Andrew would like to create a graph with n vertices and m edges, such that the Dijkstra algorithm makes as many active relaxations as possible. Help him to create such graph. To avoid nondeterminism, each time when selecting a vertex with minimal label among vertices that are not in U there must be exactly one vertex with the minimal label.

Input

The first line of the input file contains two integer numbers: n and m — the number of vertices and the number of edges in the graph Andrew would like to create (4 ≤ n ≤ 1000, n − 1 ≤ m ≤ n 2/5).

Output

Output m lines — the edges of the graph. Each line must contain three integer numbers: the beginning of the edge, the end of the edge and the weight of the edge. All weights must be non-negative and must not exceed 106 . All vertices must be reachable from vertex 1. If Dijkstra algorithm is run with s = 1 there must be maximal possible number of active relaxations among all graphs with n vertices and m edges. There must be no loops and no parallel edges.

Sample Input

4 3

Sample Output

1 2 0
1 3 1
1 4 2

HINT

题意

让你出数据卡用堆优化的迪杰斯特拉算法,要求松弛操作最多

题解

最多的情况就是每条边都会使得边松弛一次,然后构造的话,先构造一条边长为0的链,然后再从后面不断插入就好了= =

其实我感觉我说的不是很清楚,看代码吧……

注意,得插入有向边

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
int x,y,z;
};
vector<node> Q; int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int n=read(),m=read();
for(int i=;i<=n;i++)
{
Q.push_back((node){i-,i,});
m--;
}
for(int i=n;i>=;i--)
{
if(m==)
break;
for(int j=i-;j>=;j--)
{ if(m==)
break;
Q.push_back((node){i,j,i-j});
m--;
if(m==)
break;
}
if(m==)
break;
}
for(int i=;i<Q.size();i++)
{
if(Q[i].x>Q[i].y)
swap(Q[i].x,Q[i].y);
printf("%d %d %d\n",Q[i].x,Q[i].y,Q[i].z);
}
return ;
}

Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉的更多相关文章

  1. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  2. Codeforces Gym 100610 Problem E. Explicit Formula 水题

    Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  3. Gym 100531H Problem H. Hiking in the Hills 二分

    Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...

  4. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  5. codeforces Gym 100187L L. Ministry of Truth 水题

    L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  6. Codeforces Gym 102361A Angle Beats CCPC2019秦皇岛A题 题解

    题目链接:https://codeforces.com/gym/102361/problem/A 题意:给定二维平面上的\(n\)个点,\(q\)次询问,每次加入一个点,询问平面上有几个包含该点的直角 ...

  7. CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...

  8. Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造

    Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codefo ...

  9. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

随机推荐

  1. MySQL基础之第8章 视图

    8.1.视图简介 视图由数据库中的一个表,视图或多个表,视图导出的虚拟表.其作用是方便用户对数据的操作. 8.2.创建视图必须要有CREATE VIEW 和 SELECT 权限SELECT selec ...

  2. Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现

    一.前言 由于项目业务复杂,创建了多个插件并把他们放在了不同的项目中,项目使用AutoFac做的IOC:但是主项目可以注入,插件注入失败, 没有为该对象定义无参数的构造函数.下面就一步一步注入插件项目 ...

  3. HDU 5365 Run

    题意:给n个整点,问用其中若干个做顶点能够成多少个正三角形或正四边形或正五边形或正六边形. 解法:出题人说 地球人都知道整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正 ...

  4. hihoCoder 1385 A Simple Job

    #1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...

  5. [python]倒计时实现

    for num in range(5,0,-1):     time.sleep(1)     sys.stdout.flush()     sys.stdout.write('\rPlease Wa ...

  6. PICT实现组合测试用例(一)

    最近阅读了史亮老师的<软件测试实战:微软技术专家经验总结>一书,其中“测试建模”一章让我受益匪浅.想想以前的测试有多久没有花过心思放在测试用例的设计上了,一直强调“测试思想”的培养也都只是 ...

  7. python的Requests模块使用tips

    post方法提交的是表单,要用data放dict get方法请求的是参数,要用params放dict HTTP头部是大小写不敏感的

  8. 给Webkit内核的浏览器控件增加互交功能

    转载请说明出处,谢谢~~ 昨天封装了基于webkit的wke浏览器内核,做成了duilib的浏览器控件,实现了浏览功能,但是单单的浏览功能还不满足需求,在我的仿酷狗项目中乐库的功能需要与浏览器互交. ...

  9. div模拟的下拉框特效jquery

    从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...

  10. C++继承与派生的概念、什么是继承和派生

    在C++中可重用性是通过继承(inheritance)这一机制来实现的.因此,继承是C++的一个重要组成部分. 前面介绍了类,一个类中包含了若干数据成员和成员函数.在不同的类中,数据成员和成员函数是不 ...