标签:while head ring set register getch cst class har
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 100001
#define maxm 100001
#define maxw 201
using namespace std;
struct edge{
int to,dis,next;
edge(){}
edge(const int &_to,const int &_dis,const int &_next){ to=_to,dis=_dis,next=_next; }
}e[maxm*2+maxw];
int head[maxn],k;
int dis[maxn],cnt[maxn];
bool inq[maxn];
int n,m,w;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
inline void add(const int &u,const int &v,const int &w){ e[k]=edge(v,w,head[u]),head[u]=k++; }
inline bool spfa(){
memset(dis,0x3f,sizeof dis),memset(inq,false,sizeof inq),memset(cnt,0,sizeof cnt);
queue<int> q; q.push(1),inq[1]=true,dis[1]=0;
while(q.size()){
int u=q.front(); q.pop(),inq[u]=false;
for(register int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(dis[v]>dis[u]+e[i].dis){
dis[v]=dis[u]+e[i].dis,cnt[v]=cnt[u]+1;
if(cnt[v]>=n) return true;
if(!inq[v]) q.push(v),inq[v]=true;
}
}
}
return false;
}
int main(){
int t=read();
while(t--){
memset(head,-1,sizeof head);
n=read(),m=read(),w=read();
for(register int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
}
for(register int i=1;i<=w;i++){
int u=read(),v=read(),w=read();
add(u,v,-w);
}
printf("%s\n",spfa()?"YES":"NO");
}
return 0;
}
标签:while head ring set register getch cst class har
原文地址:https://www.cnblogs.com/akura/p/10976077.html