这题怎么说呢,负环上的点都不行
网上也有很多解法
我用dfs的spfa解的
我发现网上别人的代码都是用bfs的spfa写的,我就用的dfs的,快了好多
代码还看的别人的,只有中间的spfa是自己写的
我自己原来写的不知道为什么就过不了
把别人的spfa换成自己的就过了
这个是ac的:
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<algorithm> #include<stack> #include<queue> using namespace std; const int N = 209; const int MAX = 0x3f3f3f3f; int dis[N]; int p[N]; int h[N]; int cnt[N]; bool vis[N]; bool r[N]; int c[N]; struct Node { int u, v, w, next; }e[N*N]; void dfs(int x) { r[x] = 1; for(int i=h[x]; i!=-1; i=e[i].next) if(!r[e[i].v]) dfs(e[i].v); } void spfa(int u) { if(r[u]) return ; vis[u] = true; for(int i = h[u]; i != -1; i = e[i].next) { int v,w; v = e[i].v; w = e[i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u]+w; if(vis[v]) { dfs(v); return ; } else { spfa(v); } } } vis[u] = false; } int main() { int T; scanf("%d", &T); for(int tt=1; tt<=T; tt++) { int n, m; scanf("%d", &n); memset(cnt, 0, sizeof(cnt)); memset(vis, 0, sizeof(vis)); memset(r, 0, sizeof(r)); memset(dis, 0x3f3f3f3f, sizeof(dis)); for(int i=1; i<=n; i++) scanf("%d", &p[i]); scanf("%d", &m); memset(h, -1, sizeof(h)); for(int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); e[i].u = a; e[i].v = b; e[i].w = (p[b]-p[a])*(p[b]-p[a])*(p[b]-p[a]); e[i].next = h[a]; h[a] = i; } int Q; scanf("%d", &Q); for(int i=0; i<Q; i++) scanf("%d", &c[i]); printf("Case %d:\n", tt); dis[1] = 0; spfa(1); for(int i=0; i<Q; i++) { int to = c[i]; if(dis[to]==MAX || r[to] || dis[to] < 3) printf("?\n"); else printf("%d\n", dis[to]); } } return 0; }
这个是wa的,那位dalao帮我抓抓虫啊,真的苦,我看了半天楞是没看出来
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int maxn = 250; const int INF = 0x3f3f3f3f; int n,m; int cnt; struct node { int v,w; }; bool vis[maxn]; int head[maxn]; int nex[maxn]; node e[maxn*maxn]; int dis[maxn]; int busy[maxn]; bool vis2[maxn]; int c[maxn]; void adde(int u,int v,int w) { node t; t.v = v; t.w = w; e[cnt] = t; nex[cnt] = head[u]; head[u] = cnt ++; } void spfa(int u); void dfs(int u); int main() { int t; scanf("%d",&t); for(int k = 1; k <= t; ++k) { memset(vis2,0,sizeof(vis2)); memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); //memset(cn,0,sizeof(cn)); memset(nex,-1,sizeof(nex)); memset(e,0,sizeof(e)); cnt = 0; scanf("%d",&n); for(int i = 1; i <= n; ++i) scanf("%d",busy+i); scanf("%d",&m); for(int i = 0; i < m; ++i) { int a,b; scanf("%d%d",&a,&b); int c = (busy[b] - busy[a])*(busy[b] - busy[a])*(busy[b] - busy[a]); adde(a,b,c); } int Q; scanf("%d",&Q); for(int i = 0; i < Q; ++i) scanf("%d",c+i); printf("Case %d:\n", k); dis[1] = 0; spfa(1); for(int i = 0; i < Q; ++i) { if(dis[c[i]] == INF || vis2[c[i]] || dis[c[i]] < 3) printf("?\n"); else printf("%d\n",dis[c[i]]); } } return 0; } void spfa(int u) { if(vis2[u]) return ; vis[u] = true; for(int i = head[u]; i != -1; i = nex[i]) { int v,w; v = e[i].v; w = e[i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u]+w; if(vis[v]) { dfs(v); return ; } else { spfa(v); } } } vis[u] = false; } void dfs(int u) { vis2[u] = true; for(int i = head[u]; i != -1; i = nex[i]) { int v = e[i].v; if(vis2[v] == false) dfs(v); } }