Wormholes—POJ3259

简介: Wormholes—POJ3259

Problem 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

Problem solving report:

Description:路径的遍历是双向的,虫洞的传送时间是单向的,通过路径和虫洞能否使他看到最初的自己。

Problem solving: 这个题是在存在负权边的情况下是否存在负权回路,输入时一共存入2*m个边表示双向,存入 时只存入一次表示单向,利用Ford算法最后重新遍历一遍点的坐标,如果还存在最短路发生变化就会出现负权回路。

Accepted Code:

#include<stdio.h>
int main()
{
  int N;
  scanf("%d",&N); 
  while(N--){
    int dis[30000],i,k,n,m,u[30000],v[30000],w[30000],check,flag,t;
    int inf=99999999;
    int a,b,c;
    scanf("%d%d%d",&n,&m,&t);
    for(i=1;i<=2*m;){
      scanf("%d%d%d",&a,&b,&c);
      u[i]=a;
      v[i]=b;
      w[i++]=c;
      u[i]=b;
      v[i]=a;
      w[i++]=c;
    } 
    for(i=2*m+1;i<=2*m+t;i++){
      scanf("%d%d%d",&a,&b,&c);
      u[i]=a;
      v[i]=b;
      w[i]=-c;
      
    }
    for(i=1;i<=n;i++)
      dis[i]=inf;
    dis[1]=0;
    for(k=1;k<n;k++){
      check=0;
      for(i=1;i<=2*m+t;i++){
        if(dis[v[i]]>dis[u[i]]+w[i]){
          dis[v[i]]=dis[u[i]]+w[i];
          check=1;
        }   
      }
      if(check=0)
        break;
    }
    flag=0;
    for(i=1;i<=2*m+t;i++)
      if(dis[v[i]]>dis[u[i]]+w[i])
        flag=1;
    if(flag==1)
      printf("YES\n");
    else
      printf("NO\n"); 
  }

  return 0;
}
相关文章
|
6天前
|
云安全 人工智能 运维
阿里云联动百位企业安全专家,共识Agent防御最佳实践
当Agent成为新员工,你的安全边界在哪里?
1911 6
阿里云联动百位企业安全专家,共识Agent防御最佳实践
|
4天前
|
存储 人工智能 关系型数据库
阿里云AI产品与云产品最新组合套餐:Token Plan、AI coding及云服务器和建站等组合优惠价
阿里云推出全新“算力+模型+应用”一站式云与AI组合套餐活动,覆盖从个人开发者到中大型企业的全场景需求。核心亮点为分三档定价的Token Plan订阅服务,支持Qwen3.8-Max-Preview大模型调用,错峰时段最低可享0.2折优惠。活动同步推出AI Coding、智能体部署、云电脑托管、0代码建站等十余类场景化组合,搭配99元/年的普惠云服务器、88元/年的入门数据库等经典特惠产品,还为企业提供1V1定制化AI转型方案,大幅降低了不同用户群体拥抱AI的技术门槛与采购成本。
640 110
|
14天前
|
人工智能 JSON 安全
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
阿里云AI安全产品联动防御Fastjson攻击
2527 13
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
|
14天前
|
人工智能 自然语言处理 数据挖掘
Qwen3.8-Max-Preview深度全解析:2.4万亿参数旗舰MoE模型+Token Plan限时优惠完整落地指南
2026年7月,全新旗舰级混合专家大模型Qwen3.8-Max-Preview正式开放抢先体验,作为通义千问Qwen3系列规格最高、综合推理能力顶尖的新一代模型,该模型总参数量达到2.4万亿(2.4T),是当前线上可调用的原生多模态旗舰模型,综合推理水准对标海外顶级Fable 5模型,在复杂工程开发、长文档深度分析、多步骤智能体自治、跨境多语言创作、海量数据挖掘五大高难度业务场景实现跨越式性能提升。
1384 2
|
12天前
|
人工智能 前端开发 Linux
Codex 桌面版安装 + CC Switch 接入第三方 API 完整教程(2026 最新)
2026最新教程:手把手教你安装Codex桌面版,通过CC Switch v3.17.0一键接入Fenno等国产API(兼容OpenAI Responses格式),跳过账号登录,完整启用代码审查、多步任务与上下文感知功能。零基础友好,全程图文实操。(239字)
1355 2
|
16天前
|
人工智能
Qwen3.8抢先体验!正式版即将发布并开源!
千问Qwen3.8即将开源,参数达2.4T,进化速度以“天”计,实力媲美Fable 5。预览版Qwen3.8-Max已上线阿里Token Plan等平台,限时优惠:日间Credits低至1折,夜间更优,个人/团队版月付仅35元起!
1434 54
|
12天前
|
自然语言处理 测试技术 API
通义千问Qwen3.8-Max-Preview全功能解析:2.4万亿参数旗舰模型深度使用指南
在大模型技术持续迭代的当下,通义千问推出的Qwen3.8-Max-Preview作为新一代旗舰预览版模型,凭借2.4万亿参数的超大规模、多模态融合能力与全场景适配特性,成为开发者与企业用户探索AI应用的核心工具。该模型采用稀疏混合专家(MoE)架构,是通义千问首个突破万亿参数的多模态模型,可同时处理文本、图像、视频与文档等多种数据形态,在全栈代码开发、复杂逻辑推理、长文档分析与多智能体协作等场景实现跨越式升级。本文将全面拆解Qwen3.8-Max-Preview的核心功能,详解API调用流程与配置方法,覆盖多场景实战技巧,帮助用户快速掌握这款旗舰模型的使用方法,充分释放其性能潜力。
677 2