HDU 3259 Wormholes
Meaning: Give you an n, m, and t n representing the number of points, and m representing the number of two-way edges t representing the wormhole, now I want you to determine whether you can traverse the past.
Wormhole means that the side is one-way and negative weight (the input is a positive number)
Idea: Can I traverse the past point, that is, whether there is a negative ring, and apply the template decisively? The dijkstra algorithm cannot detect the negative ring.
AC code:
#include
#include
#include
#include
#include#include
using namespace std;#define maxn 520const int INF = 0x3fffffff;struct Edge{ int from,to,dist;};struct BellmanFord{ int n,m; vector
edges; vector
G[maxn]; bool inq[maxn]; int d[maxn]; int p[maxn]; int cnt[maxn]; Edge e; void init(int n) { this->n=n; for(int i=0;i
Q; memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); for(int i=0;i
d[u]+e.dist) { d[e.to]=d[u]+e.dist; p[e.to]=G[u][i]; if(!inq[e.to]) { Q.push(e.to); inq[e.to]=true; if(++cnt[e.to]>n) return true; } } } } return false; }};int main(){ int a,b,c,i,node,m,t,case1,k; bool j; scanf("%d",&case1); while(case1--) { scanf("%d %d %d",&node,&m,&t); if(node==0&&m==0)break; BellmanFord tu; tu.init(node); for(i=0;i