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的更多相关文章
随机推荐
- SilverFoxServer出炉!!
SilverFoxServer是啥?各位看官搜一下SmartFoxServer便知 是一套服务端+客户端通迅框架,快速搭建起回合制,棋牌类的联机 网页游戏 SilverFoxServer的特点包括 用 ...
- java 24 - 7 GUI之 创建多级菜单窗体
需求: 创建多级菜单 步骤: A:创建窗体对象(并设置属性和布局) B:创建菜单栏 C:创建菜单和子菜单 D:逐步添加菜单(子菜单添加到菜单中,菜单添加到菜单栏中) E:窗体中设置菜单栏(菜单栏并不是 ...
- PPT文档页数显示的增加和更新
在PPT的右下角增加页数的显示能够帮助演讲者把握进度,所以会经常遇到需要把页数显示在右下角的情况,这次在制作ppt的时候也遇到了.因此在这里总结一下设置方法. 一.在右下角显示当前页数和总页数 1)获 ...
- Android http超时选项的测试
Android通过HttpConnectionParams类为http参数设置提供了两个超时的设置选项,分别是setSoTimeout和setConnectionTimeout.初看一眼Android ...
- ubuntu12.04安装搜狗输入法和配置
1.安装 参考http://hi.baidu.com/lowkey2046/item/7ff8b33abe492bd06d15e9b6 2.配置 当前系统语言默认为英语 1)点击右上角的键盘按钮-&g ...
- JQuery学习
首先要明白一点,JQuery是一个JS的封装库,目的是为了关注点分离,让前端更加侧重于界面显示,而不是各个浏览器不同的差异性,下面是JQuery的一些常用的基本用法 一,JQuery语法 window ...
- 通俗理解T检验和F检验
来源: http://blog.sina.com.cn/s/blog_4ee13c2c01016div.html 1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总 ...
- 02传智_jbpm与OA项目_部门模块
部门模块:具有增删改查(部门)的功能. Dao层的实现: 1,定义一个DepartmentDao.java. 定义基本的数据库操作.
- ios更新UI时请尝试使用performSelectorOnMainThread方法
最近开发项目时发现联网获取到数据后,使用通知方式让列表刷新会存在死机的问题. 经过上网查找很多文章,都建议使用异步更新的方式,可是依然崩溃. 最后尝试使用performSelectorOnMainTh ...
- Linux shell文本过滤
正则表达式 --概念:一种用来描述文本模式的特殊语法 --由普通字符(例如:字符a到z),以及特殊字符(元字符,如/*?等)组成匹配的字符串 --文本过滤工具在某种模式之下,都支持正则表达式 --基本 ...