poj3259

来源:互联网 发布:c语言分割字符串函数 编辑:程序博客网 时间:2024/06/05 20:51

依旧是判环,不过需要注意的是边的设定

#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>using namespace std;struct edge{    int from;    int to;    int v;}es[6000];int n,m,w,f,s,e,t,E,d[505];bool find_loop(){    memset(d,'0',sizeof(d));    bool flag = false;    for(int i=0; i<n; i++){        for(int j=0; j<E; j++){            edge e=es[j];            if(d[e.to]>d[e.from]+e.v){                d[e.to]=d[e.from]+e.v;                flag=true;                if(i==n-1)return true;            }        }        if(!flag)break;    }    return false;}int main(){    cin>>f;    while(f--){        cin>>n>>m>>w;        E=0;        for(int i=0; i<m; i++){//路径            cin>>s>>e>>t;            es[E].from=s; es[E].to=e; es[E++].v=t;            es[E].from=e; es[E].to=s; es[E++].v=t;        }        for(int i=0; i<w; i++){//虫洞            cin>>s>>e>>t;            es[E].from=s; es[E].to=e; es[E++].v=-t;        }        if(find_loop())cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}


0 0
原创粉丝点击