听说灌水区大佬多
查看原帖
听说灌水区大佬多
544989
外星球来的人楼主2024/1/11 14:18

能不能帮我看下我的代码哪错了 题目

#include<bits/stdc++.h>
using namespace std;
const int INF=1e9;
struct E{int f;int t;int w;};
vector<E>e;
vector<int>d;
vector<int>c;
vector<bool>inQ;
bool S(int s,int n){
	d[s]=0;
	queue<int>q;
	q.push(s);
	inQ[s]=true;
	c[s]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		inQ[u]=false;
		for(vector<E>::iterator it = e.begin(); it != e.end(); ++it){
		int v = (*it).t;
		int w = (*it).w;
	if(d[u] + w < d[v]){
		d[v] = d[u] + w;
		if(!inQ[v]){
			q.push(v);
			inQ[v] = true;
			c[v]++;
			if(c[v] > n){
				return false;
			}
		}
	}
}
	}
	return true;
}
int main(){
    freopen("inequality.in","r",stdin);
    freopen("inequality.out","w",stdout);
  	int T;cin>>T;
  	while(T--){
  		int n;cin>>n;
  		e.clear();
  		d.assign(n,INF);
  		c.assign(n,0);
  		inQ.assign(n,false);
  		for(int i=0;i<n;i++){
  			string ineq;cin>>ineq;
  			int a=ineq[0]-'a';
  			int b=ineq[2]-'a';
  			int c=stoi(ineq.substr(4));
  			e.push_back({b,a,c});
  		}
  		bool h=false;
  		for(int i=0;i<n;i++){
  			if(!S(i,n)){
  				h=true;
  				break;
  			}
  		}
  		if(h){cout<<"impossible"<<endl;}
  		else{cout<<"possible"<<endl;}
  	}
    return 0;
}
2024/1/11 14:18
加载中...