判环模板题 有了上一题的经验过得很轻松

除了因为spfa还不是很熟打错了两个字母 然后debug了一小会

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; int size, head[], next[], point[], val[]; void init()
{
size = ;
memset(head, -, sizeof head);
} void add(int from, int to, int value)
{
point[size] = to;
val[size] = value;
next[size] = head[from];
head[from] = size++;
} bool spfa(int s, int n)
{
int dis[], time[];
bool vis[];
memset(dis, 0x3f, sizeof dis);
memset(time, , sizeof time);
memset(vis, false, sizeof vis); queue<int> q;
q.push(s);
dis[s] = ;
vis[s] = true;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > dis[u] + val[i]){
dis[j] = dis[u] + val[i];
if(!vis[j]){
//printf("dis[%d] = %d\n", j, dis[j]);
q.push(j);
vis[j] = true;
if(++time[j] > n) return true;
}
}
}
}
return false;
} int main()
{
int f, n, m, w;
scanf("%d", &f);
while(f--){
init();
scanf("%d%d%d", &n, &m, &w);
while(m--){
int a, b, value;
scanf("%d%d%d", &a, &b, &value);
add(a, b, value);
add(b, a, value);
}
while(w--){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
add(from, to , - value);
}
if(spfa(, n)) puts("YES");
else puts("NO");
}
return ;
}

kuangbin_ShortPath F (POJ 3259)的更多相关文章

  1. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  2. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  3. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  4. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  5. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  6. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  7. Poj(3259),SPFA,判负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  9. poj 3259 Wormholes

    题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...

随机推荐

  1. 《java笔记 day07》

    //匿名对象_1 class Car { //描述属性: String color; int number; //描述行为: void run() { System.out.println(color ...

  2. 关于dllimport的使用

    最近做一个动态加载插件的项目,插件中的dll 主要是各厂商各型号的读卡器的通用类库,stdapi.dll,WltRS.dll,有的还有进一步封装的dll,主要是为了简化通用类库的操作. 这些类库都是用 ...

  3. LoadImage函数问题

    loadimage函数加载图片类型 Value Meaning IMAGE_BITMAP Loads a bitmap. IMAGE_CURSOR Loads a cursor. IMAGE_ICON ...

  4. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

  5. hdu 2080

    ps:水题...求夹角...先求出COS,然后用acos 代码: #include "stdio.h" #include "math.h" int main() ...

  6. 模拟http或https请求,实现ssl下的bugzilla登录、新增BUG,保持会话以及处理token

    1.增加相应httpclient 需要的jar包到工程,如果是maven工程请在pom.xml增加以下配置即可: <dependency> <groupId>org.apach ...

  7. GridView导出Excel

    public void OUTEXCEL() { DataSet ds = new GW_T_DemandDAL().GetWzH(GetPersonInfoData(UserInfo), Reque ...

  8. python3爬虫再探之EXCEL(续)

    上篇介绍了xlsxwriter的用法,本来想写一下xlrd和xlwt的用法,看到这篇文章——http://blog.csdn.net/wangkai_123456/article/details/50 ...

  9. 自动打补丁Bat指令

    自动打补丁 补丁放于hotfix文件夹下 for %%i in (hotfix\*.exe) do start /wait %%i /z /u

  10. Jquery 实现banner图滚动效果

    html代码: <div id="focus"> <ul> <li><p>禅的修行应要无欲无求1</p><a hr ...