Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
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 构造题,卡迪杰斯特拉的更多相关文章
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- Codeforces Gym 100610 Problem E. Explicit Formula 水题
Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Gym 100531H Problem H. Hiking in the Hills 二分
Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- 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 ...
- Codeforces Gym 102361A Angle Beats CCPC2019秦皇岛A题 题解
题目链接:https://codeforces.com/gym/102361/problem/A 题意:给定二维平面上的\(n\)个点,\(q\)次询问,每次加入一个点,询问平面上有几个包含该点的直角 ...
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造
Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codefo ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
随机推荐
- (function(){})()这个是什么?有不明觉厉的感觉么?
今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...
- php mysql事务
这里记录一下php操作mysql事务的一些知识 要知道,MySQL默认的行为是在每条SQL语句执行后执行一个COMMIT语句,从而有效的将每条语句独立为一个事务.但是,在使用事务时,是需要执行多条sq ...
- Java异常的分类
1. 异常机制 异常机制是指当程序出现错误后,程序如何处理.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器. 传 ...
- Java SE 6 新特性: 对脚本语言的支持
2006 年底,Sun 公司发布了 Java Standard Edition 6(Java SE 6)的最终正式版,代号 Mustang(野马).跟 Tiger(Java SE 5)相比,Musta ...
- C/C++面试小知识点
1.static有什么用途. 解答: 在函数体中,一个被声明为静态的变量在这一函数被调用过程中维持其值不变. 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所有函数访问,但不能被模块外其 ...
- webdriver(python)学习笔记四——定位一组元素
webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. 定位一组对象一般用于以下场景: ...
- XTUOJ1247 Pair-Pair 预处理+暴力
分析:开个1000*1000的数组,预处理矩阵和,然后分类讨论就好 时间复杂度:O(n) #include <cstdio> #include <iostream> #incl ...
- 【转】OFBiz安全组
安全组标识 描述 用户系统级别BIZADMIN 全部商业应用程序权限组,具有全部商业应用程序管理权限,不是技术人员权限. bizadmin FLEXADMIN 缓存管理组,具有全部颗粒的权限. fle ...
- 《Linux命令行与shell脚本编程大全》 第三章 学习笔记
第三章:基本的bash shell命令 bash程序使用命令行参数来修改所启动shell的类型 参数 描述 -c string 从string中读取命令并处理他们 -r 启动限制性shell,限制用户 ...
- 关于LyX的一些小问题
LyX转HTML(MS WORD)的脚本elyxer的更新: 首先从官网下载最新版的软件包.解压后,在命令行下python install.py执行安装(直接双击install.py应该也可以),py ...