lightoj 1108 - Instant View of Big Bang(SPFA负环)

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 xy 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



给你一个有向图,问你是否存在负环,如果有负环,就把可以形成负环的起点输出出来,如果没有的话输出 'impossible'。

正向建边可以找出负环上的边,如果反向建边的话就可以找到负环可以到哪些点,也就是负环的起点。

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define endl '\n'
using namespace std;
const int inf = 1e9;
int n,m,flag;
int a[1100],dis[1100],b[1100];
int vis[1100],cnt[1100];
struct edge
{
    int to,cost;
};
vector<edge> e[1100];
void dfs(int x)
{
    vis[x] = 1;
    for(int i=0;i<e[x].size();i++)
        if(!vis[e[x][i].to])
            dfs(e[x][i].to);
}
void SPFA(int s)
{
    memset(b,0,sizeof(b));
    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));
    queue<int> q;
    for(int i=1;i<=n;i++)
    {
        dis[i] = 0;
        q.push(i);
    }
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        b[t] = 0;
        for(int i=0;i<e[t].size();i++)
        {
            int x = e[t][i].to;
            if(vis[x])
                continue;
            if(dis[x] > dis[t] + e[t][i].cost)
            {
                dis[x] = dis[t] + e[t][i].cost;
                if (b[x] == 0)
                {
                    q.push(x);
                    b[x] = 1;
                    cnt[x]++;
                    if(cnt[x] > n)
                        dfs(x);
                }
            }
        }
    }
}
int main(void)
{
    int T,i,j;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<=n;i++)
            e[i].clear();
        for(i=1;i<=m;i++)
        {
            edge t;
            int x,y;
            scanf("%d%d%d",&x,&y,&t.cost);
            x++,y++;
            t.to = x;
            e[y].push_back(t);
        }
        flag = 0;
        SPFA(1);
        printf("Case %d:",cas++);
        for(i=1;i<=n;i++)
            if(vis[i])
            {
                printf(" %d",i-1);
                flag = 1;
            }
        if(flag == 0)
            printf(" impossible");
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值