Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
2015-08-06 18:44
344 查看
[b]Problem H. Hard Test[/b]
Time Limit: 20 Sec
Memory Limit: 256 MB
[b]题目连接[/b]
http://codeforces.com/gym/100342/attachments
[b]Description[/b]
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.
[b]Input[/b]
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).
[b]Output[/b]
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.
[b]Sample Input[/b]
4 3
[b]Sample Output[/b]
1 2 0
1 3 1
1 4 2
[b]HINT[/b]
[b]题意[/b]
让你出数据卡用堆优化的迪杰斯特拉算法,要求松弛操作最多
[b]题解:[/b]
最多的情况就是每条边都会使得边松弛一次,然后构造的话,先构造一条边长为0的链,然后再从后面不断插入就好了= =
其实我感觉我说的不是很清楚,看代码吧……
注意,得插入有向边
[b]代码:[/b]
Time Limit: 20 Sec
Memory Limit: 256 MB
[b]题目连接[/b]
http://codeforces.com/gym/100342/attachments
[b]Description[/b]
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.
[b]Input[/b]
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).
[b]Output[/b]
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.
[b]Sample Input[/b]
4 3
[b]Sample Output[/b]
1 2 0
1 3 1
1 4 2
[b]HINT[/b]
[b]题意[/b]
让你出数据卡用堆优化的迪杰斯特拉算法,要求松弛操作最多
[b]题解:[/b]
最多的情况就是每条边都会使得边松弛一次,然后构造的话,先构造一条边长为0的链,然后再从后面不断插入就好了= =
其实我感觉我说的不是很清楚,看代码吧……
注意,得插入有向边
[b]代码:[/b]
#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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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=2;i<=n;i++) { Q.push_back((node){i-1,i,0}); m--; } for(int i=n;i>=2;i--) { if(m==0) break; for(int j=i-2;j>=1;j--) { if(m==0) break; Q.push_back((node){i,j,i-j}); m--; if(m==0) break; } if(m==0) break; } for(int i=0;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 0; }
相关文章推荐
- 开始app开发的工作小结
- 【linux高级程序设计】(第十五章)UDP网络编程应用 4
- 内存池(一)
- Screen position out of view frustum
- Hdoj 2795 Billboard
- EventBus实例教程(二)
- Detect the target host network whether can ping through or not(shell script)
- 错误No Persistence provider for EntityManager named
- HDU 5360 Hiking
- Microsoft C#基础知识复习
- 在云环境下利用Teraproc集群即服务实现R代码的GPU加速
- C# 如何通过委托进行窗口传值
- EventBus实例教程(一)
- C 将一个整数输出为内存中的存储形式
- hdu 5353 Average(2015 Multi-University Training Contest 6)
- nyoj488&&hdu1016 素数环(DFS)
- App数据重点分析的4个方面
- iOS访问相册以及拍照保存至相册的实现
- MFC右键时产生菜单
- Appium1.4 的简介、安装及其使用真机测试(iOS版)