Description

  Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.

  题目就是求最短路问题,但是可能存在负环,存在的话就需要标记所有负环上的点,输出?。

  (但是坑的是判断到负环直接退出居然也AC了。。。。。。)

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=MaxN*MaxN;
const int INF=10e8; struct Edge
{
int to,next,cost;
}; Edge E[MaxM];
int head[MaxN],Ecou;
bool vis[MaxN];
int couNode[MaxN];
bool cir[MaxN]; void init(int N)
{
Ecou=; for(int i=;i<=N;++i)
head[i]=-,vis[i]=,cir[i]=;
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} void dfs(int u)
{
cir[u]=; for(int i=head[u];i!=-;i=E[i].next)
if(!cir[E[i].to])
dfs(E[i].to);
} bool SPFA(int lowcost[],int N,int start)
{
queue <int> que;
int u,v,c; for(int i=;i<=N;++i)
lowcost[i]=INF,couNode[i]=;
lowcost[start]=; que.push(start);
vis[start]=;
couNode[start]=; while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=; // !!! for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(cir[v])
continue; if(lowcost[v]>lowcost[u]+c)
{
lowcost[v]=lowcost[u]+c; if(!vis[v])
{
vis[v]=;
que.push(v); ++couNode[v]; if(couNode[v]>N)
dfs(v);
}
}
}
} return ;
} int ans[MaxN];
int val[MaxN]; inline int cube(int x)
{
return x*x*x;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,Q,M;
int a,b;
int cas=; scanf("%d",&T); while(T--)
{
scanf("%d",&N);
init(N); for(int i=;i<=N;++i)
scanf("%d",&val[i]); scanf("%d",&M);
while(M--)
{
scanf("%d %d",&a,&b);
addEdge(a,b,cube(val[b]-val[a]));
} SPFA(ans,N,); scanf("%d",&Q); printf("Case %d:\n",cas++); while(Q--)
{
scanf("%d",&a); if(cir[a] || ans[a]< || ans[a]==INF)
printf("?\n");
else
printf("%d\n",ans[a]);
}
} return ;
}

(简单) LightOJ 1074 Extended Traffic,SPFA+负环。的更多相关文章

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

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

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

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

  3. LightOJ 1074 Extended Traffic SPFA 消负环

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

  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)

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

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

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

  7. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  8. SPFA(负环) LightOJ 1074 Extended Traffic

    题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...

  9. LightOJ - 1074 Extended Traffic(标记负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

随机推荐

  1. 关于项目刚才还能运行的,重启Myeclipse就不能运行(报错)的解决方法

    这个是以为内存不足引起的,就是打开MyEclipse的时候,因为内存不足,没有加载完整的项目,这个时候需要重启电脑,即可解决问题.

  2. UML类图图示样例

    下图来自<大话设计模式>一书:

  3. cocos2d-lua SDK接入

    1.lua 调用Java函数 1.1 在java中创建一个静态函数(比如在org.cocos2dx.lua.AppActivity.java中)名为Login public static void m ...

  4. zzuli 1919 数列划分

    题面: Description 晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了...他想问你这个分出来的和最大的一段的和 ...

  5. UVALive 2147 Push!!(队列实现DP)

    就我的理解来说这个题,本质上是一个DP题,不应该说是搜索,因为我的做法是把表格中所有的数据都找到,使用队列暴力来遍历出所有状态,因为题目中的数据范围小,所有耗时也小. 首先分析箱子是一个被动物体,人是 ...

  6. Java 堆内存(Heap)[转]

    将jvm内存很不错的文章,转自 堆(Heap)又被称为:优先队列(Priority Queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复 ...

  7. circularprogressbar/smoothprogressbar开源视图使用学习

    github地址:https://github.com/castorflex/SmoothProgressBar 多彩圆形进度条和多彩水平进度条 colors.xml 定义变化的颜色内容,用gplus ...

  8. PAT1008

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B The highest building in our city has on ...

  9. jvisualvm

    f the fonts used by VisualVM are hard to read, switching the LaF might help.  Try for example  'visu ...

  10. StretchAnimation伸缩动画.

    原理是继承animation  然后改变他的margintop  和marginbottom  形成2个效果 ExpandTopAnimation public class ExpandTopAnim ...