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的更多相关文章
随机推荐
- 【Unity】Update()和FixedUpdate()
Update()每帧调用,FixedUpdate()以指定频率被调用. 可以在 Edit -> project settings -> Time -> Fixed Timestep ...
- C#中Abstract和Virtual
C#中Abstract和Virtual 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Vi ...
- IT菜鸟的第一天
小弟愚钝,被别人影响,打算入IT行业试试水的深浅,俗话说技不压身,多会一种就多一条路子,抱着这种求知的心态我就开始的我在汉企的IT生涯! 第一天无非就是简介,对IT行业的介绍,反正听得我挺懵的,不过介 ...
- AFN 处理网络哪些事(轻松掌握AFN网络顶级框架)
AFN 一.什么是AFN 全称是AFNetworking,是对NSURLConnection的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的githu ...
- Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录
在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 线上数据库备份场景:每周日执行一次全量备份,然后每天下午1点执行MySQLdump增量备份 ...
- Kali linux渗透测试常用工具汇总1
1.ProxyChains 简介:代理工具.支持HTTP/SOCKS4/SOCK5的代理服务器,允许TCP/DNS通过代理隧道. 应用场景:通过代理服务器上网. 配置:/etc/proxychains ...
- Tomcat 开启 SSL
生成keystore /usr/java/default/bin/keytool -genkey -alias tomcat -keyalg RSA -keystore ~/tomcat.keysto ...
- PAT 1029. 旧键盘(20)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输入的文字.以及实际 ...
- notes:spm多重比较校正
SPM做完统计后,statistical table中的FDRc实际上是在该p-uncorrected下,可以令FDR-correcred p<=0.05的最小cluster中的voxel数目: ...
- WPF DatePicker默认显示当前日期
WPF的日历选择控件默认为当前日期,共有两种方法,一种静态,一种动态. 静态的当然写在DatePicker控件的属性里了,动态的写在对应的cs文件里,具体请看下面. 1.方法一: my ...