poj 3259 Wormholes SPFA // Bellman-ford

//最水的模版题,记下来以后忘了可以看看。

//SPFA

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=505;
int n, m, w, d[maxn], inqueue[maxn], cnt[maxn];
struct node{
    int e[maxn], v[maxn], sum;
}edge[maxn];

void add_edge(int s, int e, int v){
    edge[s].e[edge[s].sum]=e;
    edge[s].v[edge[s].sum]=v;
    edge[s].sum++;
}

void init(){
    int i, x, y, c;
    scanf("%d%d%d", &n, &m, &w);
    for(i=1; i<=n; i++){
        edge[i].sum=0;
        cnt[i]=0;
        inqueue[i]=0;
        d[i]=INF;
    }
    for(i=0; i<m; i++){
        scanf("%d%d%d", &x, &y, &c);
        add_edge(x, y, c);
        add_edge(y, x, c);
    }
    for(i=0; i<w; i++){
        scanf("%d%d%d", &x, &y, &c);
        add_edge(x, y, -c);
    }
}

bool spfa(int st){
    int i, head;
    queue<int > q;
    d[st]=0;
    inqueue[st]=1;
    q.push(st);
    cnt[st]=1;
    while(!q.empty()){
        head=q.front();
        q.pop();
        inqueue[head]=0;
        for(i=0; i<edge[head].sum; i++){
            if(d[edge[head].e[i]]>d[head]+edge[head].v[i]){
                d[edge[head].e[i]]=d[head]+edge[head].v[i];
                if(!inqueue[edge[head].e[i]]){
                    inqueue[edge[head].e[i]]=1;
                    cnt[edge[head].e[i]]++;
                    if(cnt[edge[head].e[i]]>=n)
                    return false;
                    q.push(edge[head].e[i]);
                }
            }
        }
    }
    return true;
}
int main(){
    //freopen("1.txt", "r", stdin);
    int T;
 scanf("%d", &T);
 while(T--){
  init();
  if(spfa(1))printf("NO\n");
  else printf("YES\n");
 }
    return 0;
}


//Bellman-ford
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0xfffffff;
int n, m, w, num, d[505];
struct node{
    int s, e, v;
}edge[5300];void Add_edge(int s, int e, int v){
    edge[num].s=s;
    edge[num].e=e;
    edge[num].v=v;
    num++;
}
void init(){
    int i, x, y, c;
    num=0;
    scanf("%d%d%d", &n, &m, &w);
    for(i=0; i<m; i++){
        scanf("%d%d%d", &x, &y, &c);
        Add_edge(x, y, c);
        Add_edge(y, x, c);
    }
    for(i=0; i<w; i++){
        scanf("%d%d%d", &x, &y, &c);
        Add_edge(x, y, -c);
    }
    for(i=1; i<=n; i++)
    d[i]=INF;
    d[1]=0;
}
bool bellman(){
    int i, j;
    for(i=1; i<n; i++){
        for(j=0; j<num; j++){
            if(d[edge[j].s]+edge[j].v<d[edge[j].e])
                d[edge[j].e]=d[edge[j].s]+edge[j].v;
        }
    }
    for(j=0; j<num; j++){
        if(d[edge[j].s]+edge[j].v<d[edge[j].e])
        return false;
    }
    return true;
}
int main(){
    //freopen("1.txt", "r", stdin);
 int i, T;
 scanf("%d", &T);
 while(T--){
  init();
  if(bellman())printf("NO\n");
  else printf("YES\n");
 }
 return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值