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

利用在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. 第二十四篇configparser(**)

    configparser模块 config:配置,parser:解析.字面意思理解configparser模块就是配置文件的解析模块. 来看一个好多软件的常见文档格式如下: [DEFAULT] # 标 ...

  2. cpp语言程序设计教程第七章的一道编程题

    题目如下 按下列要求实现一个有关学生成绩的操作. 该类名为Student. (1)每个学生的信息包含有姓名(字符数组)和成绩(int型). (2)共有5个学生,用对象数组表示. (3)计算出5个学生中 ...

  3. React远程服务

    http://web1.dev5.net:3002/cloud/start?server=exf2&name=zhangxiaocong http://web1.dev5.net:3002/c ...

  4. cocos2d-x环境搭建 摘自百度文库

    cocos2d-x环境搭建 引言:笔者在网上寻觅了很多资料,最终发现了这份实际可用的文档,供大家参考.源地址:http://wenku.baidu.com/view/93f7b0f1102de2bd9 ...

  5. [转]Hexo博客添加访问统计 - 记录

    引入不蒜子 <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"> ...

  6. js计算当前日期上一个月和下一个月

    /**         * 获取上一个月         *         * @date 格式为yyyy-mm-dd的日期,如:2014-01-25         */        funct ...

  7. 用 xampp 在ubuntu 下配置php 运行环境 lampp

      在linux下搭建php的运行环境,可以分别安装apache.mysql .php .phpmyadmin,也可以安装xampp(apache.mysql .php .phpmyadmin)集成包 ...

  8. ELK6.2.4集群

    ELK6.2.4集群安装使用 https://www.cnblogs.com/frankdeng/p/9139035.html 一 简介 Elasticsearch是一个高度可扩展的开源全文搜索和分析 ...

  9. 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set

    题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...

  10. BZOJ4668 冷战(并查集)

    显然可以用LCT维护kruskal重构树.或者启发式合并维护kruskal重构树的倍增数组虽然多了个log也不一定比LCT慢吧. 当然这里的kruskal重构树几乎只是把树上的边权换成了点权,并不重要 ...