kuangbin_ShortPath F (POJ 3259)

时间:2022-09-07 18:30:51

判环模板题 有了上一题的经验过得很轻松

除了因为spfa还不是很熟打错了两个字母 然后debug了一小会

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; int size, head[], next[], point[], val[]; void init()
{
size = ;
memset(head, -, sizeof head);
} void add(int from, int to, int value)
{
point[size] = to;
val[size] = value;
next[size] = head[from];
head[from] = size++;
} bool spfa(int s, int n)
{
int dis[], time[];
bool vis[];
memset(dis, 0x3f, sizeof dis);
memset(time, , sizeof time);
memset(vis, false, sizeof vis); queue<int> q;
q.push(s);
dis[s] = ;
vis[s] = true;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > dis[u] + val[i]){
dis[j] = dis[u] + val[i];
if(!vis[j]){
//printf("dis[%d] = %d\n", j, dis[j]);
q.push(j);
vis[j] = true;
if(++time[j] > n) return true;
}
}
}
}
return false;
} int main()
{
int f, n, m, w;
scanf("%d", &f);
while(f--){
init();
scanf("%d%d%d", &n, &m, &w);
while(m--){
int a, b, value;
scanf("%d%d%d", &a, &b, &value);
add(a, b, value);
add(b, a, value);
}
while(w--){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
add(from, to , - value);
}
if(spfa(, n)) puts("YES");
else puts("NO");
}
return ;
}