题目链接:http://lightoj.com/volume_showproblem.php?problem=1074

题意:有n个城市,每个城市有一个拥堵值a[i],m条单向路u到v,从u到v所需时间是(a[v]-a[u])^3, q个查找,查找从起点1到点u的最短时间是多少;如果不能到达或者时间少于3输出-1;

可能存在负环,所以用spfa最合适。负环上的点是不能到达的,所以要注意;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 515 using namespace std; int G[N][N], cnt[N], n, vis[N], dist[N], a[N], flag; void Init()
{
flag = ;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = INF;
G[i][i] = vis[i] = cnt[i] = ;
dist[i] = INF;
}
} void SPFA()
{
cnt[] = ;
dist[] = ;
queue<int>Q;
Q.push();
vis[] = ;
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = ; if(cnt[p] > n*)///说明存在负环,循环次数足够多,就可以让环上的点进队列的次数都大于n;
{
flag = ;
return ;
} for(int i=; i<=n; i++)
{
if(dist[i] > dist[p] + G[p][i])
{
dist[i] = dist[p] + G[p][i];
if(!vis[i])
{
vis[i] = ;
cnt[i]++;
Q.push(i);
}
}
}
}
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T--)
{
int m, q, u, v; scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &a[i]); Init(); scanf("%d", &m);
for(int i=; i<=m; i++)
{
scanf("%d %d", &u, &v);
G[u][v] = (a[v]-a[u])*(a[v]-a[u])*(a[v]-a[u]);
} SPFA(); scanf("%d", &q); printf("Case %d:\n", t++); while(q--)
{
scanf("%d", &u);
if(cnt[u]<n && dist[u] != INF && dist[u]>=)
printf("%d\n", dist[u]);
else
printf("?\n");
}
}
return ;
}

LightOj1074 - Extended Traffic(SPFA最短路)的更多相关文章

  1. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  2. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  3. LightOJ-1074 Extended Traffic 最短路问题 注意连通性

    题目链接:https://cn.vjudge.net/problem/LightOJ-1074 题意 给一图 求最短路 若最短路<3或没有最短路,则输出'?' 思路 首先注意到可能存在负环,所以 ...

  4. LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)

    题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...

  5. LightOJ - 1074 Extended Traffic (SPFA+负环)

    题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...

  6. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  7. Light OJ 1074:Extended Traffic(spfa判负环)

    Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...

  8. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  9. NOIP2013 华容道 (棋盘建图+spfa最短路)

    #include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...

随机推荐

  1. XCOJ 1102 (树形DP+背包)

    题目链接: http://xcacm.hfut.edu.cn/oj/problem.php?id=1102 题目大意:树上取点.父亲出现了,其儿子包括孙子...都不能出现.给定预算,问最大值. 解题思 ...

  2. topcoder SRM 592 DIV2 LittleElephantAndPermutationDiv2

    #include <iostream> #include <vector> #include <algorithm> #include <iterator&g ...

  3. USACO 5.4 Telecowmunication(最大流+枚举)

    面对最小割之类的题目,完全木想法... 枚举+最大流..复杂度很大了...居然很快的就过了.. /* ID: cuizhe LANG: C++ TASK: telecow */ #include &l ...

  4. USACO 5.4 Betsy's Tour(暴力)

    水过,水过,这个程序跑7,跑5分钟左右把... /* ID: cuizhe LANG: C++ TASK: betsy */ #include <iostream> #include &l ...

  5. 基于nginx tomcat redis分布式web应用的session共享配置

    一.前言 nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存.web server负载均衡等功能,由于其轻量级.高性能.高可靠等特点在互联网项目中有着非常普遍的应用,相关 ...

  6. 一致性 hash 算法

    consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...

  7. order by id asc得出的排序是什么原理

    我们要用order by id asc得出的排序应该是,4,好了原理就这么简. sql实现方法,代码如下: : 代码如下: $sql ="Select 字段 from 表名 where id ...

  8. 增加Activity Monitor中的作业保存数量

    在Master Server的注册表中加入如下两个键值即可: (1500的单位是小时)  

  9. 分布式架构高可用架构篇_08_MyCat在MySQL主从复制基础上实现读写分离

    参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...

  10. NOJ 1651 Red packet(二分)

    [1651] Red packet 时间限制: 1000 ms 内存限制: 65535 K 问题描述 New Year is coming! Our big boss Wine93 will dist ...