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

    避免类名混淆: 区分有包名的类,如果一个源文件引入了两个包中同名的类,那么在使用该类时,不允许省略包名,如引入了tom.jiafei包中的AA类和sun.com包中的AA类,那么程序在使用AA类时必须 ...

  2. Windows英文版GitHub客户端使用操作流程图文攻略教程现没中文版

    Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.作为一个程序员,我们需要掌握其用法. 作为开源代码库以及版本控制系统,Github目前拥有140 ...

  3. 精通CSS version2笔记之⒈选择器

    1.常用的选择器:①元素选择器   指定希望应用样式的元素.比如:p {color:#fff;}②后代选择器   寻找特定元素或者元素的后代. 比如:body p{color:#ccc;}   这个选 ...

  4. uva146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  5. DataTable详解,以及dataview

    原文地址:http://www.cnblogs.com/moss_tan_jun/archive/2010/09/20/1832131.html 得到DataTable 得到DataTable有许多方 ...

  6. 关于a标签和submit标签

    a如果没有连接“#”:“javascript:void(0)”;或“(胡乱写一堆)” 这两个标签点击都有刷新功能,所以会清空你的数据.

  7. PAT 1016. 部分A+B (15)

    正整数A的"DA(为1位整数)部分"定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的"6部分"PA是66,因为A中有 ...

  8. usb驱动开发14之设备生命线

    直接看代码吧. /*-------------------------------------------------------------------*/ /** * usb_submit_urb ...

  9. Java 基础命名空间

    java.lang (提供利用 Java 编程语言进行程序设计的基础类)java.lang.annotation(提供了引用对象类,支持在某种程度上与垃圾回收器之间的交互)java.lang.inst ...

  10. TelephonyManager类与PhoneStateListener

    public class TelephonyManager extends Object java.lang.Object      android.telephony.TelephonyManage ...