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).

Input

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.

Output

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.

Example
Input
1
4 4
1 2 1
3 4 2
2 3 3
1 3 1
Output
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的更多相关文章

随机推荐

  1. There was an internal API error.的解决办法

    1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...

  2. HTML 学习笔记 CSS(选择器3)

    CSS 属性选择器 属性选择器可以根据元素的额属性以及属性值来选择元素 例子1 如果 你希望把包含title的所有元素变成红色 *[title] {color:red} 例子2 与上面类似 可以只对有 ...

  3. JSTL中的TLD配置和使用。

    一,JSTL介绍: JSTL标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一 ...

  4. 夯实基础之php学习-2提高篇

    1,Jpgraph, 详见Php图形化jpgraph 2,文件系统 文件的操作步骤:打开文件->操作文件->关闭文件 打开文件fopen(filename,mode) 关闭文件fclose ...

  5. js checkbox 选中判断

    var isSelect = ""; isSelect = $("#tblImgList" + " input[type='checkbox']&qu ...

  6. C# Chrome内核

    C#将WebBowser控件替换为Chrome内核 摘要 由于最近要做一个浏览器式的软件,其中有不少地方需要使用到jQuery和BootStrap,但是在C#中,默认的WebBrowser控件默认使用 ...

  7. Hangfire项目实践

    Hangfire项目实践分享 Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget ...

  8. Integer.valueof(null)报错

    原文  http://javacat360.iteye.com/blog/2024378 主题 Java 昨天,一同事问我一个问题,估计是他前段日子面试遇到的 问题很简单,String.valueof ...

  9. React Native开发技术周报1

    (一).资讯 1.React Native 0.21版本发布,最新版本功能特点,修复的Bug可以看一下已翻译 重要:如果升级 Android 项目到这个版本一定要读! 我们简化了 Android 应用 ...

  10. JQuery实现资讯上下滚动悬停效果

    第一步:使用repeater绑定一个table. <table width="530" id="rollBar"> <asp:Repeater ...