poj 3259 Wormholes(Bellman-Ford)

poj 3259 Wormholes

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output
Lines 1.. F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

题目大意:一个人在宇宙中旅行。有两种路径能够走。第一种是普通的宇宙。是双向的,而且耗时。

另外一种是虫洞。单向的,而且能够使时间倒流。如今问在宇宙中是否有一条路,能够使得男主通过虫洞返回出发前的时间。

解题思路:读入普通路径的时候,记得双向。读入虫洞路径的时候记得是单向的。而且权值为负。图建完了,就是BF推断是否有负环。有负环则能够穿越回过去,否则不行。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;

const int INF = 0x3f3f3f3f;
const int M = 6000;
const int N = 600;
typedef long long ll;
int n, m, w, s;
struct Edge {  
    int from, to, dist;  
};  
vector<Edge> edges;  

int vis[N], d[N], rec[N];
int L[N];
void init() {  
    for (int i = 0; i <= n; i++) vis[i] = 0;
    edges.clear();  
}  

void addEdge(int from, int to, int dist) {  
    edges.push_back((Edge){from, to, dist});  
}
void input() {
    scanf("%d %d %d", &n, &m, &w);
    int a, b, c;
    for (int i = 0; i < m; i++) {
        scanf("%d %d %d", &a, &b, &c);  
        addEdge(a, b, c);
        addEdge(b, a, c);
    }
    for (int i = 0; i < w; i++) {
        scanf("%d %d %d", &a, &b, &c);  
        addEdge(a, b, -c);
    }
}

int BF() {
    for (int i = 0; i < n; i++) d[i] = INF; 
    d[0] = 0;
    int a, b;
    for (int i = 0; i < n - 1; i++) { //迭代n - 1次
        for (int j = 0; j < edges.size(); j++) { //检查每条边
            a = edges[j].from, b = edges[j].to;
            if (d[a] < INF) {
                d[b] = min(d[a] + edges[j].dist, d[b]); //松弛
            }
        }
    }
    for (int i = 0; i < edges.size(); i++) { //若n - 1次更新后,还能更新。则有负环
        a = edges[i].from, b = edges[i].to;
        if (d[b] > d[a] + edges[i].dist) {
            return 1;
        }
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int A, B;
        init();
        input();
        if (BF()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

转载于:https://www.cnblogs.com/ljbguanli/p/7365630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值