题目链接:http://poj.org/problem?id=2679

嗯,思路清晰,先DFS看是不是通路,接着就是SPFA找最短路(路是费用,费用相同就比较路的长度)。

超哥的代码还有一点问题,初始化最小费用为0,也能AC,我不信,if语句也好像写错了,只能说明这个题目数据水。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std; #define MAXN 1105
#define MAXM 5005
#define INF 0x3f3f3f3f struct Edge
{
int v,len,fee,next;
Edge() {}
Edge(int vv,int ll,int ff, int nn):v(vv),len(ll),fee(ff),next(nn) {}
} e[*MAXM]; int n,m;
int dept,dest;
int h[MAXN], le;
int minfee[MAXN], minFee[MAXN];
int vis[MAXN];
int d[MAXN], cnt[MAXN]; void add(int u, int v,int w,int fuv,int fvu)
{
e[le] = Edge(v, w, fuv, h[u]);
h[u] = le++;
e[le] = Edge(u, w, fvu, h[v]);
h[v] = le++;
} bool dfs(int x) //判定从x是否可达终点
{
vis[x] = true;
if(x == dest) return true;
if(minfee[x] == INF) return false;
for(int i = h[x]; i != -; i = e[i].next)
{
if(!vis[e[i].v] && minfee[x] == e[i].fee)
{
if(dfs(e[i].v)) return true;
vis[e[i].v] = false;
}
}
return false;
} bool spfa(int x)
{
memset(d,INF,sizeof(d));
memset(vis,false,sizeof(vis));
memset(cnt, , sizeof(cnt));
memset(minFee, INF, sizeof(minFee));
queue<int> q;
q.push(x);
vis[x] = true;
cnt[x] = ;
d[x] = ;
minFee[x] = ;
while(!q.empty())
{
int cur = q.front();
q.pop();
vis[cur] = false;
for(int i = h[cur]; i != -; i = e[i].next)
{
if(minfee[cur] != e[i].fee) continue;
int v = e[i].v, w = e[i].len;
if(minFee[v] > minFee[cur] + minfee[cur] ||(minFee[v] == minFee[cur] + minfee[cur]&&d[v] > d[cur] + w)) //先判断费用更小,再判断距离更小
{
minFee[v] = minFee[cur] + minfee[cur];
d[v] = d[cur] + w;
if(!vis[v])
{
vis[v] = true;
if(++cnt[v] > n) //成环
{
if(dfs(v)) return false; //可达终点
else continue; //不可达终点
}
q.push(v);
}
}
}
}
return true;
} int main()
{ while(~scanf("%d%d%d%d", &n,&m,&dept,&dest))
{
memset(h,-,sizeof(h));
memset(minfee,INF,sizeof(minfee));
le = ;
char cmd[];
int u,v,w,fuv,fvu;
for(int i = ; i < m; i++)
{
scanf("%s",cmd);
sscanf(cmd, "(%d,%d,%d[%d]%d)", &u,&v,&fuv,&w,&fvu);
add(u,v,w,fuv,fvu);
minfee[u] = min(minfee[u], fuv);
minfee[v] = min(minfee[v], fvu);
} memset(vis,false,sizeof(vis));
bool VOID = !dfs(dept);
if(VOID) //没有路径可达
{
printf("VOID\n");
continue;
}
bool UNBOUND = !spfa(dept); //可达路径中存在负权费用环路
if(UNBOUND) printf("UNBOUND\n");
else printf("%d %d\n", minFee[dest], d[dest]);
} return ;
}

Poj(2679),SPFA,二级比较的更多相关文章

  1. Poj(2679),SPFA,邻接表(主流写法)

    题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...

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

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

  3. poj 2679 Adventurous Driving(SPFA 负环)

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

  4. poj 1511(spfa)

    ---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...

  5. poj 3268(spfa)

    http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...

  6. poj 1511(SPFA+邻接表)

    题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...

  7. Invitation Cards POJ 1511 SPFA || dij + heap

    http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...

  8. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...

  9. poj 3259Wormholes (spfa最短路径)

    #include<stdio.h> #include<string.h> #include<limits.h> #include<queue> usin ...

随机推荐

  1. C++Primer 第十三章

    //1.当定义一个类时,我们显示地或隐式地指出在此类型的对象(注意这里是此类型的对象,而不包括此类型的指针)拷贝,移动,赋值,销毁时做什么.一个类通过定义五种特殊的成员函数来控制这些操作:拷贝构造函数 ...

  2. JS小练习 留言功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. csuoj 1390: Planting Trees

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1390 1390: Planting Trees Time Limit: 1 Sec  Memory ...

  4. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  5. Oracle角色

    一 .3种标准角色 Qracle为了兼容以前的版本,提供了三种标准的角色(role):CONNECT.RESOURCE和DBA. 1. CONNECT Role(连接角色) 临时用户,特别是那些不需要 ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON DirectShow (Delphi Prism)

    zw版[转发·台湾nvp系列Delphi例程]HALCON DirectShow (Delphi Prism) namespace DirectShow_Prism;interfaceuses Sys ...

  7. 「Ruby && Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)

    error message:           no such file .... 安装 gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/lo ...

  8. Limit the query running time with Resource limit facility (RLF)

    If you need to limit the query(package,plan) running time, but the JCL/JOB TIME parameters doesn't w ...

  9. jboss-as 目录结构(转)

    jboss-as 目录结构(Directory Structure) Directory Description bin Contains startup, shutdown and other sy ...

  10. React的一个简单示例

    首发:个人博客,更新&纠错&回复 React的核心是定义组件类,组件有三个要素:状态.行为.界面. 1.渲染状态到界面:状态由组件对象的state属性持有,从状态到界面的渲染工作由组件 ...