gym923B
Even though he isn't a student of computer science, Por Costel the pig has started to study Graph Theory. Today he's learning about Bellman-Ford, an algorithm that calculates the minimum cost path from a source node (for instance, node 1) to all the other nodes in a directed graph with weighted edges. Por Costel, utilizing his scarce programming knowledge has managed to scramble the following code in C++, a variation of the Bellman-Ford algorithm:

You can notice a lot of deficiencies in the above code. In addition to its rudimentary documentation, we can see that Por Costel has stored this graph as an array of edges (the array
). An edge is stored as the triplet
signifying an edge that spans from
to
and has weight
. But even worse is the fact that the algorithm is SLOW!
As we want our hooved friend to walk away with a good impression about computer science, we want his code to execute as FAST as possible. In order to do so, we can modify the order of edges in the array
so that the while loop executes a small number of times.
Given a directed graph of
nodes and
edges, you are asked to produce an ordering of the edges such that the Bellman-Ford algorithm written by Por Costel should finish after at most two iterations of the while loop(that is, the program should enter the while loop at most twice).
The first line of the file algoritm.in will contain an integer
, the number of test cases.
Each of the
test cases has the following format: on the first line, there are two numbers
and
(
), the number of nodes and the number of edges in the graph respectively.
The next
lines describe the edges, each containing three integers
signifying there is an edge from node
to node
with weight
(
)
It is guaranteed that node
has at least one outgoing edge.
The graph may contain self loops and/or multiple edges.
The output file algoritm.out should contain
lines representing the answers to each test case.
For each test case you should output a permutation of numbers from
to
, representing the order of the edges you want in Por Costel's array of edges
.
The edges are considered indexed by the order in which they are given in the input (the
-th edge read is the edge with index
).
If there are multiple solutions, you are allowed to print any of them.
1
4 4
1 2 1
3 4 2
2 3 3
1 3 1
1 4 2 3
dijiestra存访问路径
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int inf=1<<29;
typedef pair<int,int> PII;
struct edge
{
int v,val,i;
};
priority_queue<PII,vector<PII>,greater<PII> >q;
vector<edge>graph[100100];
vector<int>num;
int n,m,u,v,val,T;
int d[100100],used[200100],use[200100];
void dijiestra()
{
memset(used,0,sizeof(used));
q.push(PII(0,1));
while(!q.empty())
{
PII x=q.top();q.pop();
int u=x.second;
if(used[u]) continue;
used[u]=1;
for(int i=0;i<graph[u].size();i++)
{
edge x=graph[u][i];
int v=x.v,val=x.val;
if(d[v]>d[u]+val){d[v]=d[u]+val;num.push_back(x.i);use[x.i]=1;q.push(PII(d[v],v));}
}
}
// for(int i=1;i<=n;i++) cout<<d[i]<<" ";
// cout<<endl;
for(int i=0;i<num.size();i++) printf("%d ",num[i]);
for(int i=1;i<=m;i++) if(!use[i]) printf("%d ",i);
cout<<endl;
}
int main()
{
freopen("algoritm.in","r",stdin);
freopen("algoritm.out","w",stdout);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=2;i<=n;i++) d[i]=inf;
memset(use,0,sizeof(use));
num.clear();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&val);
edge e;
e.v=v,e.val=val,e.i=i;
graph[u].push_back(e);
}
dijiestra();
for(int i=1;i<=n;i++)
graph[i].clear();
}
fclose(stdin);
fclose(stdout);
return 0;
}
gym923B的更多相关文章
随机推荐
- Hibernate之组件映射
1:为什么要使用组件映射 答:建立关系数据模型的一个重要原则是在不会导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系.以员工信息为例,员工信息中有员工的家庭地址信息,如果把地址信息 ...
- Android:关于声明文件中android:process属性说明
笔者在学习Android Service组件的过程中碰到了一个问题,就是在Android应用的声明文件Manifest.xml中有时候会对相关的服务标签设置一个android:process=&quo ...
- HTML 学习笔记 CSS(轮廓)
轮廓(outline)是绘制于元素周围的一条线 位于边框边缘的外围 可起到突出元素的作用 CSS outline属性规定元素轮廓的样式 颜色和宽度 话不多说 来几个例子 看一看 1:在元素周围画线 & ...
- BZOJ 1257: [CQOI2007]余数之和sum
1257: [CQOI2007]余数之和sum Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3769 Solved: 1734[Submit][St ...
- css 九宫格
http://jsfiddle.net/webtiki/kQXkt/ http://jsfiddle.net/webtiki/MpXYr/3/embedded/ https://www.web-tin ...
- mongodb分布式查询
分布式查询:mongodb的分布式模型分为replica set和sharded cluster. sharded集群中将read根据sharding key(分片键)转发到指定的shard节点,re ...
- Java 8 Lambda表达式探险
为什么? 我们为什么需要Lambda表达式 主要有三个原因: > 更加紧凑的代码 比如Java中现有的匿名内部类以及监听器(listeners)和事件处理器(hand ...
- codevs2806 红与黑
难度等级:白银 codevs2806 红与黑 题目描述 Description 有一个矩形房间,覆盖正方形瓷砖.每块瓷砖涂成了红色或黑色.一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一, ...
- 工作随笔——Intellij_idea-14官方快捷键中文版
听说Intellij Idea好几年了.因为快捷键的原因,所以一直没有放弃eclipse.上周末抽了点时间,用google翻译+自己实践翻译了一下官方的快捷键. 基本做完的时候在百度文库上突然搜索到一 ...
- c++ 指针(二)
函数指针 可以使用算法的地址传递给方法,传递之前要先完成以下工作 1.获取函数的地址 2.声明一个函数指针 3.使用函数指针来调用函数 1.获取函数的地址,只要使用函数名就可以 Fun2(Fun1); ...