algoritm.in / algoritm.out

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

题意就是一个傻逼写了个最短路,问你怎么将输入的graph的边排序,使得他的最短路只跑一次。

显然先跑在最短路径树上的边,再跑其他的边,就只需要一次了。

必须用堆dijkstra,好像卡了spfa。

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
#define INF 1000000000007ll
#define N 100010
#define M 200010
struct Point{ll d;int u;
Point(const ll &X,const int &Y){d=X;u=Y;}
Point(){}};
int T,n,m;
int cnt;
bool operator < (Point a,Point b){return a.d>b.d;}
priority_queue<Point>q;
int v[M],__next[M],first[N],w[M],e;
int fa[N],fam[N];
ll d[N];
void AddEdge(int U,int V,int W)
{
v[++e]=V;
w[e]=W;
__next[e]=first[U];
first[U]=e;
}
bool vis[N],intree[M];
void dijkstra(int S)
{
for(int i=1;i<=n;++i) d[i]=INF;
d[S]=0; q.push(Point(0,S));
while(!q.empty())
{
Point x=q.top(); q.pop();
if(!vis[x.u])
{
vis[x.u]=1;
for(int i=first[x.u];i;i=__next[i])
if(d[v[i]]>d[x.u]+(ll)w[i])
{
d[v[i]]=d[x.u]+(ll)w[i];
fa[v[i]]=x.u;
intree[fam[v[i]]]=0;
fam[v[i]]=i;
intree[i]=1;
q.push(Point(d[v[i]],v[i]));
}
}
}
}
void dfs(int U)
{
for(int i=first[U];i;i=__next[i])
if(intree[i])
{
++cnt;
printf("%d%c",i,cnt==m ? '\n' : ' ');
dfs(v[i]);
}
}
int main()
{
freopen("algoritm.in","r",stdin);
freopen("algoritm.out","w",stdout);
//freopen("b.in","r",stdin);
int x,y,z;
scanf("%d",&T);
for(;T;--T)
{
cnt=e=0;
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
memset(__next,0,sizeof(__next));
memset(first,0,sizeof(first));
memset(fa,0,sizeof(fa));
memset(fam,0,sizeof(fam));
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
memset(intree,0,sizeof(intree));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&z);
AddEdge(x,y,z);
}
dijkstra(1);
dfs(1);
for(int i=1;i<=m;++i)
if(!intree[i])
{
++cnt;
printf("%d%c",i,cnt==m ? '\n' : ' ');
}
}
return 0;
}

【Heap-dijkstra】Gym - 100923B - Por Costel and the Algorithm的更多相关文章

  1. 【找规律】Gym - 100923L - Por Costel and the Semipalindromes

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  2. 【分块打表】Gym - 100923K - Por Costel and the Firecracker

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  3. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  4. 【并查集】Gym - 100923H - Por Costel and the Match

    meciul.in / meciul.out Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight ...

  5. 【动态规划】Gym - 100923A - Por Costel and Azerah

    azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...

  6. 【带权并查集】Gym - 100923H - Por Costel and the Match

    裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...

  7. 【NOI导刊200908模拟试题02 题4】【二分+Dijkstra】 收费站

    Description 在某个遥远的国家里,有n个城市.编号外1,2,3,-,n. 这个国家的政府修建了m条双向的通路.每条公路连接着两个城市.沿着某条公路,开车从一个城市到另一个城市,需要花费一定的 ...

  8. 【拓扑排序】【线段树】Gym - 101102K - Topological Sort

    Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...

  9. 【每日dp】 Gym - 101889E Enigma 数位dp 记忆化搜索

    题意:给你一个长度为1000的串以及一个数n 让你将串中的‘?’填上数字 使得该串是n的倍数而且最小(没有前导零) 题解:dp,令dp[len][mod]为是否出现过 填到第len位,余数为mod 的 ...

随机推荐

  1. 忘记mysq rootl密码后解决办法

    如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下 ...

  2. Linux下设置防火墙(开启端口)

    1.修改文件/etc/sysconfig/iptables 在文件中加入如下内容,目的是对外界开放7001端口 -A RH-Firewall-1-INPUT -m state --state NEW ...

  3. 【bzoj3926- [Zjoi2015]诸神眷顾的幻想乡】广义sam

    题意:给定一棵树,每个节点有一个颜色,问树上有多少种子串(定义子串为某两个点上的路径),保证叶子节点数<=20.n<=10^5 题解: 叶子节点小于等于20,考虑将每个叶子节点作为根把树给 ...

  4. HDU 4320 Arcane Numbers 1 (数论)

    A - Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  5. HDU 2036 改革春风吹满地 (数学)

    题目链接 Problem Description " 改革春风吹满地, 不会AC没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)" 话说部分学生心态极好,每天就知道 ...

  6. python学习笔记 协程

    在学习异步IO模型前,先来了解协程 协程又叫做微线程,Coroutine 子程序或者成为函数,在所有语言中都是层级调用,比如a调用b,b调用c.c执行完毕返回,b执行完毕返回,最后a执行完毕返回 所以 ...

  7. Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题

    首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...

  8. c#使用selenium截图,操作dom元素

    using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...

  9. C#后台调用js方法无效果,未解决。

    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script> ...

  10. mac废纸篓清空的心得、mac设置不睡眠不待机不锁屏、如何快速锁屏待机睡眠、mac重启、mac学习的必备软件-城

    mac废纸篓清空: 1.使用废纸篓的清空废纸篓,清空所有包括被锁定的文件: 2.使用“磁盘工具”的“修复磁盘权限”,修复完成再操作清空废纸篓: 3.使用cleanmymac软件“垃圾清理”和“擦除器” ...