求助各位大佬!!!68分,7、8、9、10RE,啥情况
查看原帖
求助各位大佬!!!68分,7、8、9、10RE,啥情况
331817
lirunjia楼主2022/8/13 20:11
#include<bits/stdc++.h>
using namespace std;
const int N = 2010, M = 3010;
int n, m;
int h[N], w[M], e[M], ne[M], idx;
int dist[N], cnt[N];
bool st[N];
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool spfa(int n)
{
	memset(cnt,0,sizeof cnt);
	memset(dist,0x3f,sizeof dist);
	memset(st,0,sizeof st);
    queue<int> q;
    dist[1]=0;
    st[1] = true;
    q.push(1);  
    while (q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;

                if (cnt[j] >= n) return true;
                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		idx=0;
		memset(w,0,sizeof w);
		memset(e,0,sizeof e);
		memset(ne,0,sizeof ne);
		scanf("%d%d", &n, &m);
		memset(h, -1, sizeof h);
		while (m -- )
    	{
        	int a, b, c;
        	scanf("%d%d%d", &a, &b, &c);
        	add(a, b, c);
        	if(c>=0) add(b, a, c);
		}
		if(spfa(n)) cout<<"YES"<<endl;
    	else cout<<"NO"<<endl;
	}
    return 0;
}
2022/8/13 20:11
加载中...