一开始重新建图搞的。然后参照了别人的博客。这个解法比较好

利用在SPFA维护入队次数。入队次数大于节点数目的位于负环。

那么负环中的点如何DFS到终点。(SPFA从起点开始如果能找到入队大于N那么一定可以从起点到这个点)那么就NOBOUND;

VOID和输出ANS就比较容易了

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 2000
#define MAXM 20010
const int INF = 0x3f3f3f3f;
int N,M,A,B;
struct node
{
int u,v,next;
int length,weight;
}edge[MAXM];
int head[MAXN],cnt;
int num[MAXN],d[MAXN],l[MAXN];
bool inq[MAXN],used[MAXN];
int q[MAXM];
bool found;
void insert(int u ,int v,int weight,int length)
{
if (head[u] != - && weight > edge[head[u]].weight) return;
if (head[u] != - && weight < edge[head[u]].weight) head[u] = -;
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].weight = weight;
edge[cnt].length = length;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void read()
{
memset(head,-,sizeof(head));
cnt = ;
for (int i = ; i < M; i++)
{
int u ,v, weightl,weightr,length;
// if (i != M - 1)
scanf("(%d,%d,%d[%d]%d) ",&u,&v,&weightl,&length,&weightr);
// else scanf("(%d,%d,%d[%d]%d)",&u,&v,&weightl,&length,&weightr);
//printf("%d %d %d %d %d \n",u,v,weightl,length,weightr);
insert(u,v,weightl,length);
insert(v,u,weightr,length);
}
}
bool SPFA(int s)
{
int front = ,rear = ;
memset(inq,false,sizeof(inq));
memset(num,,sizeof(num));
memset(d,0x3f,sizeof(d));
memset(l,0x3f,sizeof(l));
inq[s] = true;
q[] = s;
d[s] = l[s] = ;
num[s] = ;
while (front !=rear)
{
int u = q[front] ;
front = (front + ) % MAXM;
inq[u] = false;
for (int i = head[u]; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] > d[u] + edge[i].weight ||( (d[v] == d[u] + edge[i].weight) && (l[v] > l[u] + edge[i].length)))
{
d[v] = d[u] + edge[i].weight;
l[v] = l[u] + edge[i].length;
if (!inq[v])
{
inq[v] = true;
num[v]++;
q[rear] = v;
rear = (rear + ) % MAXM;
if (num[v] > N + ) return false;
}
}
}
}
return true;
}
void dfs(int cur)
{
used[cur] = true;
if (cur == B) {found = true; return;}
for (int i = head[cur]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (!used[v]) dfs(v);
}
}
int main()
{
// freopen("sample.txt","r",stdin);
while (scanf("%d%d%d%d ",&N,&M,&A,&B) != EOF)
{
found = false;
read();
bool flag = SPFA(A);
if (d[B] == INF) puts("VOID");
else
{
if (flag) printf("%d %d\n",d[B],l[B]);
else
{
for (int i = ; i < N; i++)
if (num[i] > N)
{
memset(used,false,sizeof(used));
found = false;
dfs(i);
if (found) break;
}
if (found || num[B] > N) puts("UNBOUND");
else printf("%d %d\n",d[B],l[B]);
}
}
}
return ;
}

UVALIVE 3307 Adventurous Driving的更多相关文章

  1. POJ 2679:Adventurous Driving(SPFA+DFS)

    http://poj.org/problem?id=2679 Adventurous Driving Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. poj 2679 Adventurous Driving(SPFA 负环)

    /* - - 这题做了一天.....粗心害死人啊 题目描述恶心 数据更恶心... 先处理一下能走的边 能走的点(到这建边从终点跑一下.) 然后就是SPFA了 注意负环的判断 */ #include&l ...

  3. UVALive 4868 Palindrometer 暴力

    F - Palindrometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. mac系统下mysql开机启动总是3307

    修改了mysql的my.cnf可还是不行,启动后就是3307,必须关掉再启动. 觉得可能是mac系统在哪里写死了开机启动项. http://queforum.com/mysql/1012987-mys ...

  8. tensorfolw配置过程中遇到的一些问题及其解决过程的记录(配置SqueezeDet: Unified, Small, Low Power Fully Convolutional Neural Networks for Real-Time Object Detection for Autonomous Driving)

    今天看到一篇关于检测的论文<SqueezeDet: Unified, Small, Low Power Fully Convolutional Neural Networks for Real- ...

  9. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

随机推荐

  1. Java进阶知识点1:白捡的扩展性 - 枚举值也是对象

    一.背景 枚举经常被大家用来储存一组有限个数的候选常量.比如下面定义了一组常见数据库类型: public enum DatabaseType { MYSQL, ORACLE, SQLSERVER } ...

  2. Tensorflow编程基础之Mnist手写识别实验+关于cross_entropy的理解

    好久没有静下心来写点东西了,最近好像又回到了高中时候的状态,休息不好,无法全心学习,恶性循环,现在终于调整的好一点了,听着纯音乐突然非常伤感,那些曾经快乐的大学时光啊,突然又慢慢的一下子出现在了眼前, ...

  3. rcnn caffe matlab 配置完成 14.04 cuda 7.0

    http://blog.csdn.net/real_myth/article/details/42672381 各种痛苦.实验室网速还是龟速. 莫名其妙的错误. gcc还降级到4.7,opencv 3 ...

  4. get? post? put? delete? head? trace? options? http请求方法

    http1.1协议里面定义了八种请求方法: get:用作获取,读取数据 post:向指定的资源提交数据 put:更新,向指定的资源上传一个内容,比如说:更新一个用户的头像或者替换掉已有的一个视频 de ...

  5. 【题解】ZJOI2013蚂蚁寻路

    这题强呀……打了10+30暴力之后苦想1h并不会做……于是去看题解.看题解的时候又莫名各种看错,结果看了好久才懂……记录一下血泪史吧. 这题不难发现走出来的图形就是一个高低高低的城堡型图案,命名为高峰 ...

  6. Elasticsearch 5.2.1Cluster 搭建

    1.安装java cd ~ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...

  7. c++(类)构造函数、复制构造函数

    复制构造函数是一种特殊的构造函数,它的作用是用一个已经存在的对象去初始化另一个对象.一般情况下不需要自行定义复制构造函数,系统默认提供一个逐个复制成员值的复制构造函数. 何时要使用呢? 1.将新对象初 ...

  8. NET中IL理解(转)

    .NET CLR 和 Java VM 都是堆叠式虚拟机器(Stack-Based VM),也就是說,它們的指令集(Instruction Set)都是採用堆叠运算的方式:执行时的资料都是先放在堆叠中, ...

  9. spring boot修改内置容器tomcat的服务端口

    方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...

  10. SDK登录cognos

    通过SDK登录cognos 一种是拼xml,如这里的实现https://github.com/cosysoft/cognos-tools/blob/master/src/com/ibm/cognos/ ...