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. 转 : 如何用sys as sysdba权限连接数据库进行EXP/IMP

    使用sys as sysdba权限进行EXP/IMP与其它用户稍有不同,详细内容如下(摘自metalink) Applies to: Oracle Server - Enterprise Editio ...

  2. http請求瀏覽器的緩存機制

    轉載自:http://kb.cnblogs.com/page/73901/ 流程 当资源第一次被访问的时候,HTTP头部如下 (Request-Line) GET /a.html HTTP/1.1 H ...

  3. TexturePacker license Key免费获取方式

    TexturePacker是一款功能非常强大的图片制作工具.是一款付费软件,但是TexturePacker的作者Andreas Löw先生也给出获得免费key 的方法...大家可以到这个网站去申请 h ...

  4. linux 查看磁盘、文件夹、文件大小(df du)

    du 查看文件夹大小 1.查看当前文件夹中所有文件夹及其子文件夹的大小,注意是文件夹大小,不是文件 # du -h -rw-r--r-- 1 root root 82785865 6月 9 15:53 ...

  5. Java Interrupt Related

    In Java, the main process can have several threads at a time, but only a few of them can run concurr ...

  6. Unity3D项目优化(转)

    前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章,而且陈述的都是既有的事实,因而给自己“文(dou)学(bi)”加工留下的 ...

  7. linux shell 远程执行命令

    经常要部署多台服务器上面的应用,如果一个个机器的登录太麻烦. 所有就想到编写一个脚本来部署不同的服务器 前提条件: 配置ssh免登陆 如果不会的请参加我的另外一篇文章 http://blog.csdn ...

  8. TM3、4波段GeoTiff数据计算NDVI

      源码: 1: PRO TIFF_NDVI,F1,F2,FOUT 2: F1 = DIALOG_PICKFILE(TITLE = 'B4 TIFF',FILTER='*.TIF',/READ) 3: ...

  9. MySQL常用命令总结2

    USE db_name; //使用(打开)数据库 SELECT DATABASE(); //查看当前打开的数据库 CREATE TABLE tb_name( column_name data_type ...

  10. Apache的安装

    Apache的安装: 注:本例只截取需要注意的截图,其它默认则不显示. 1.       服务器信息可以按照默认配置,如果服务器的80端口没被其他服务器程序占据.可选“for All Users,on ...