您的位置:首页 > 其它

bellman-ford 算法 spfa 算法 实例 poj 3259

2012-07-05 20:54 288 查看
#include<iostream>
#include<stdio.h>
using namespace std;
#define max 100000
struct side{
int start;
int end;
int value;
};
side a[5000];
int ver[500];
int m1,m2,m3;
bool bellman(){
for(int i=1;i<=m1;i++){
ver[i]=max;
}
ver[1]=0;
int flag;
int h=0;
for(;h<m1-1;h++){
int g=1;
flag=0;
for(;g<=m2+m3;g++){
if(ver[a[g].start]+a[g].value<ver[a[g].end]){
ver[a[g].end]=ver[a[g].start]+a[g].value;
flag=1;
}
if(ver[a[g].end]+a[g].value<ver[a[g].start]&&a[g].value>=0){
ver[a[g].start]=ver[a[g].end]+a[g].value;
flag=1;
}
}
if(flag==0)
return true;
}
if(h>=m1-1&&flag==1)
return false;
}

int main(){
int n;
cin>>n;
while(n--){
scanf("%d%d%d",&m1,&m2,&m3);
//cin>>m1>>m2>>m3;

int temp1,temp2,temp3;
int j=1;
for(;j<=m2;j++){
// cin>>a[j].start>>a[j].end>>a[j].value;
scanf("%d%d%d",&temp1,&temp2,&temp3);
a[j].start=temp1;
a[j].end=temp2;
a[j].value=temp3;
}
for( ;j<=m2+m3;j++){
scanf("%d%d%d",&temp1,&temp2,&temp3);
//cin>>a[j].start;
//cin>>a[j].end;
a[j].start=temp1;
a[j].end=temp2;
//     int temp;
//   cin>>temp;
a[j].value=-temp3;
}
if(m1==1){
printf("YES\n");
//      cout<<"YES";
//         if(n>0)
//           printf("\n");
//    cout<<endl;
continue;
}
if(bellman())
printf("NO\n");
//     cout<<"NO";
else

// cout<<"YES";
printf("YES\n");
/*  if(n>0)
cout<<endl;
}*/
}
return 0;
}


bellman-ford 是对dijkstra算法的优化。其主要的特点就是能够判断图中是否存在负环。

负环即一个环路中的节点间的距离和为负数。

如果存在负环,那么其始点到各点的最短路径和会不断地更新,即该环路中不存在最短距离。

bellman-ford 的基本思想:

1. 用struct结构体来表示边;其内容有:起点,终点,边权。

2. 所有节点的权值初始化为无穷大,始点赋为0。

3. 由边来更新节点的权值;如果始点+边<终点,则更新终点,有多少条边就做多少次。

4. 将第三步循环做,直到不存在更新(跳出,且图的始点到各点的最短路径得出)或循环满了n-1次后,还存在更新(图中存在负环)。

5. 判断是否存在负环。

spfa 即对bellman-ford 得一个优化;主要是针对于在更新节点时,不需要将每一个节点都遍历一次。

一般spfa 都用邻接表的方法来存储图。

spfa 主要思想:

1. 从原点开始,遍历每一个与之相连的节点,将每一个更新的且不在队列中的节点入队,以便下一次的更新。

2. 提取队首的节点,重复1,知道队列为空。

3. 如果当存在节点第n次入队时,则可判定该图存在负环。

A - Wormholes
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8


Sample Output

NO
YES


Hint

For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: