zoukankan      html  css  js  c++  java
  • spfa_dfs找负环

    luogu

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    inline void in(int &p,char c=getchar(),bool f=0)
    {
    	while((c<'0' or c>'9') and c!='-')
    		c=getchar();
    	p=0;
    	if(c=='-')
    		f=1,c=getchar();
    	while(c>='0' and c<='9')
    		p=(p<<3)+(p<<1)+c-'0',c=getchar();
    	if(f)
    		p=-p;
    }
    int n,m;
    struct way
    {
    	int to;
    	int cost;
    };
    vector <way> v[200001];
    bool fuhuan,vis[200001];
    long long dis[200001];
    void spfa_dfs(int x)
    {
    	vis[x]=1;
    	for(unsigned int i=0;i<v[x].size();i++)
    	{
    		if(dis[x]+v[x][i].cost<dis[v[x][i].to])
    		{
    			dis[v[x][i].to]=dis[x]+v[x][i].cost;
    			if(vis[v[x][i].to])
    			{
    				fuhuan=1;
    				//vis[x]=0;
    				return ;
    			}
    			spfa_dfs(v[x][i].to);
    		}
    	}
    	vis[x]=0;
    }
    int main()
    {
    	//freopen("shuru.txt","r",stdin);
    	int t;
    	in(t);
    	while(t--)
    	{
    		in(n);in(m);
    		for(int i=1;i<=n;i++)
    			v[i].clear();
    		memset(vis,0,sizeof(vis));
    		memset(dis,0,sizeof(dis));
    		fuhuan=0;
    		for(int i=0;i<m;i++)
    		{
    			int x,y,c;
    			in(x);in(y);in(c);
    			way a;
    			a=(way){y,c};
    			v[x].push_back(a);
    			if(c>=0)
    			{
    				a.to=x;
    				v[y].push_back(a);
    			}
    		}
    		for(int i=1;i<=n;i++)
    			if(!fuhuan)
    				spfa_dfs(i);
    		if(fuhuan)
    			printf("YE5
    ");
    		else
    			printf("N0
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    php获取OS信息
    坑爹的IE quirk模式
    [转]mysql privileges
    [转]VSFTPD的设置选项
    php性能分析工具xhprof
    php扩展安装
    又是万恶的IE....6
    ASP.NET操作文件(文件夹)简单生成html操作示例
    asp.net文件操作类
    c++/clr与c#的性能比较
  • 原文地址:https://www.cnblogs.com/syhien/p/7760250.html
Copyright © 2011-2022 走看看