1108 - Instant View of Big Bang
Time Limit: 2 second(s) Memory Limit: 32 MB

Have you forgotten about wormholes? Oh my god! Ok, let me explain again.

A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties:

  1. Wormholes are one-way only.
  2. The time it takes to travel through a wormhole is negligible.
  3. A wormhole has two end points, each situated in a star system.
  4. A star system may have more than one wormhole end point within its boundaries.
  5. Between any pair of star systems, there is at most one wormhole in each direction.
  6. There are no wormholes with both end points in the same star system.

All wormholes have a constant time difference between their end points. For example, a specific wormhole may cause the person traveling through it to end up 15 years in the future. Another wormhole may cause the person to end up 42 years in the past.

A brilliant physicist wants to use wormholes to study the Big Bang. Since warp drive has not been invented yet, it is not possible for her to travel from one star system to another one directly. Thiscan be done using wormholes, of course.

The scientist can start her journey from any star system. Then she wants to reach a cycle of wormholes somewhere in the universe that causes her to end up in the past. By traveling along this cycle a lot of times, the scientist is able to go back as far in time as necessary to reach the beginning of the universe and see the Big Bang with her own eyes. Write a program to help her to find such star systems where she can start her journey.

Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each case starts with a blank line. The next line contains two numbers n and m . These indicate the number of star systems (1 ≤ n ≤ 1000) and the number of wormholes (0 ≤ m ≤ 2000). The star systems are numbered from 0 to n-1. For each wormhole a line containing three integer numbers x, y and t is given. These numbers indicate that this wormhole allows someone to travel from the star system numbered x to the star system numbered y, thereby ending up t (-1000 ≤ t ≤ 1000) years in the future or past, a negative integer denotes past, positive integer denotes future.

Output

For each case, print the case number first. Then print the star systems (in ascending order) where she can start her journey. If no such star system is found, print 'impossible'.

Sample Input

Output for Sample Input

2

3 3

0 1 1000

1 2 15

2 1 -42

4 4

0 1 10

1 2 20

2 3 30

3 0 -60

Case 1: 0 1 2

Case 2: impossible


Problem Setter: Jane Alam Jan
题意:让你找所有的可以走入负环的点;
思路:spfa找负环;
这题的关键不是去判断负环,而是咋样去找那些点,我们可以知道,假如是负环的话用spfa去更新会无限循环。如果某个点能到达到达负环,并且它不在负环的内部,我们咋判断这个点,由于负环的内部即使边的方向改变了,这个负环还是不会变的,那么原来指向这个负环的边就会变成,负环指向那个点,那么当你找到负环然后dfs找点就能将所有符合要求的点求出。其实关键就是反向建图;
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<math.h>
7 using namespace std;
8 typedef struct pp
9 {
10 int from;
11 int to;
12 int pre;
13 int cost;
14 } ss;
15 const int N=1e8;
16 ss node[6000];
17 ss nn[6000];
18 int id[6000];
19 int id1[6000];
20 int ask[6000];
21 bool flag[6000];
22 int cnt[6000];
23 int sum[6000];
24 int d[6000];
25 int yy[6000];
26 void add(int from,int to,int cost,int cc);
27 void add1(int from,int to,int cost,int cc);
28 void dfs(int n);
29 void spfa(int n);
30 int main(void)
31 {
32 int i,j,k;
33 scanf("%d",&k);
34 int s;
35 int n,m;
36 for(s=1; s<=k; s++)
37 {
38 memset(ask,0,sizeof(ask));
39 scanf("%d %d",&n,&m);
40 memset(id,-1,sizeof(id));
41 memset(id1,-1,sizeof(id1));
42 int ans=0;
43 for(i=0; i<m; i++)
44 {
45 int x,y,z;
46 scanf("%d %d %d",&x,&y,&z);
47 add(y,x,z,ans);
48 ans++;
49 }
50 spfa(n);
51 int xx=0;
52 for(i=0; i<n; i++)
53 {
54 if(ask[i])
55 {
56 sum[xx++]=i;
57 }
58 }
59 printf("Case %d: ",s);
60 if(xx==0)
61 printf("impossible\n");
62 else
63 {
64 printf("%d",sum[0]);
65 for(i=1; i<xx; i++)
66 printf(" %d",sum[i]);
67 printf("\n");
68 }
69 }
70 return 0;
71 }
72 void add(int from,int to,int cost,int cc)
73 {
74 node[cc].from=from;
75 node[cc].to=to;
76 node[cc].cost=cost;
77 node[cc].pre=id[from];
78 id[from]=cc;
79 }
80 void spfa(int n)
81 {
82 int i,j,k;
83 memset(yy,0,sizeof(yy));
84 for(i=0; i<n; i++)
85 {
86 if(!ask[i]&&!yy[i])
87 {
88 memset(flag,0,sizeof(flag));
89 memset(cnt,0,sizeof(cnt)) ;
90 fill(d,d+n,N);
91 yy[i]=1;
92 queue<int>que;
93 que.push(i);
94 flag[i]=true;
95 cnt[i]++;
96 que.push(i);
97 while(!que.empty())
98 {
99 int vv=que.front();
100 que.pop();
101 flag[vv]=false;
102 int kk=id[vv];
103 while(kk!=-1)
104 {
105 int gg=node[kk].to;
106 if(!ask[gg])
107 {
108 if(d[gg]>d[vv]+node[kk].cost)
109 {
110 d[gg]=d[vv]+node[kk].cost;
111 if(flag[gg]==false)
112 {
113 que.push(gg);
114 cnt[gg]++;
115 flag[gg]=true;
116 if(cnt[gg]>n)
117 {
118 dfs(gg);
119 while(!que.empty())
120 {
121 que.pop();
122 }
123 break;
124 }
125 }
126 }
127 }
128 kk=node[kk].pre;
129 }
130 }
131 }
132 }
133 }
134 void dfs(int n)
135 {
136 ask[n]=1;
137 int xx=id[n];
138 while(xx!=-1)
139 {
140 int kk=node[xx].to;
141 if(!ask[kk])
142 {
143 dfs(kk);
144 }
145 xx=node[xx].pre;
146 }
147 }

1108 - Instant View of Big Bang的更多相关文章

  1. navicat连接oracle报错ORA-12737: Instant Client Light: unsupported server character set CHS16GBK”

    原文如下http://blog.163.com/cp7618@yeah/blog/static/7023477720142154449893/?COLLCC=1318255100& 这个工具可 ...

  2. iphone dev 入门实例1:Use Storyboards to Build Table View

    http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/ Creating Navig ...

  3. Table View滑动时报错

    学习表视图(Table View)的应用时,自己写了个demo,最后表格出来了,可是滑动时报错了,报错如下: 这是我ViewController.m部分的代码: #import "ViewC ...

  4. Linux平台ORACLE INSTANT客户端安装

    下载安装文件 先去ORACLE官方网站下载所需版本的Instant Client Package 和 Instant Client Package - SQL*Plus安装包,(千万注意版本) htt ...

  5. Android Studio 2.1及其以上版本中的instant run功能 介绍

    Android Studio 2.0及其以后版本中的instant run功能 介绍 转 https://blog.csdn.net/zy987654zy/article/details/514961 ...

  6. android上instant app介绍 类似于微信小程序

    android上instant app介绍 类似于微信小程序instant app 是谷歌推出的类似于微信小程序(或者说小程序类似于instant app)的一项技术,用户无须安装应用,用完就走,同时 ...

  7. Android之MVC——Model通知View去更新(实用)

    下面两段标红加深的代码是重点: import android.app.Activity; import android.os.Bundle; import android.view.View; imp ...

  8. Instant Messaging for OpenERP v7

    This module is based on the socket.io real time implementation. It inherit Of the modules web_longpo ...

  9. Instant Messaging for Business: Your 10 Best Options

    Instant Messaging for Business: Your 10 Best Options By Iaroslav Kudritskiy It's probably not a surp ...

随机推荐

  1. 监督学习&非监督学习

    监督学习 1 - 3 - Supervised Learning  在监督学习中,数据集中的每个例子,算法将预测得到例子的""正确答案"",像房子的价格,或者溜 ...

  2. 日常Java 2021/9/26 (二柱升级版)

    package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...

  3. 论文解读(GraRep)《GraRep: Learning Graph Representations with Global Structural Information》

    论文题目:<GraRep: Learning Graph Representations with Global Structural Information>发表时间:  CIKM论文作 ...

  4. 编程之美Q1

    题目 和数书页有点类似,就直接数吧 #include<iostream> using namespace std; class q1 { public: size_t func(size_ ...

  5. Android项目的settings.gradle和build.gradle

    gradle构建的项目中的build.gradle和settings.gradle文件 build.gradle 浅析(一) 史上最全的Android build.gradle配置教程 Android ...

  6. Oracle中IS TABLE OF的使用

    IS TABLE OF :指定是一个集合的表的数组类型,简单的来说就是一个可以存储一列多行的数据类型. INDEX BY BINARY_INTEGER:指索引组织类型 BULK COLLECT :指是 ...

  7. 使用 scipy.fft 进行Fourier Transform:Python 信号处理

    摘要:Fourier transform 是一个强大的概念,用于各种领域,从纯数学到音频工程甚至金融. 本文分享自华为云社区<使用 scipy.fft 进行Fourier Transform:P ...

  8. OpenGL ES2.0 入门经典例子

    原文链接地址:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial 免责申明(必读!):本博客提供的所有教程的翻译原稿 ...

  9. git 基本命令及idea集成使用

    目录 git基本命令使用 设置签名 gitHub 服务配置秘钥 上传代码 更新代码 分支管理 bat脚本更新 idea集成git git基本命令使用 设置签名 签名和秘钥大多数是一起设置的,设置后一般 ...

  10. SourceTree Git可视化管理工具通过 ssh 密钥登录

    整个流程分三步:① 生成SSH密钥:② Github/Gitee/Coding 代码托管平台绑定公钥:③ SourceTree 拉取代码 1.生成 SSH 密钥 这里直接使用 SourceTree 来 ...