标签:size nod turn color write std 水过 putchar return
AC这道题已经是第二遍了,第一次是用别人一个叫DP的代码水过的,尽管我都不知道什么事DP;今天突然有了灵感,自己又写了一遍,用双向SPFA,第一次就过了,和大家分享一下;
#include <bits/stdc++.h> using namespace std; #define N 100000 inline int read() { int x = 0,ff = 1;char ch = getchar(); while(!isdigit(ch)) { if(ch == ‘-‘) ff = -1; ch = getchar(); } while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch ^ 48); ch = getchar(); } return x * ff; } inline void write(int x) { if(x < 0) putchar(‘-‘),x = -x; if(x > 9) write(x / 10); putchar(x % 10 + ‘0‘); } int a,b,ans; struct node { int y,next; }e1[N<<1],e2[N<<1]; int tot1,lin1[N<<1]; int tot2,lin2[N<<1]; int mon[N<<1],mi[N<<1],ma[N<<1]; int head,tail,q[N<<1],vis[N<<1]; void add1(int xx,int yy) { e1[++tot1].y = yy; e1[tot1].next = lin1[xx]; lin1[xx] = tot1; } void add2(int xx,int yy) { e2[++tot2].y = yy; e2[tot2].next = lin2[xx]; lin2[xx] = tot2; } void SPFA1(int s) { memset(vis,false,sizeof(vis)); memset(mi,0x3f,sizeof(mi)); head = 1,tail = 1; q[1] = s; mi[s] = mon[s]; for(head = 1;head <= tail;++head) { int x = q[head]; vis[x] = false; for(int i = lin1[x],y;i;i = e1[i].next) { if(mi[y = e1[i].y] > mi[x]) { mi[y] = min(mon[y],mi[x]); if(!vis[y]) { vis[y] = true; q[++tail] = y; } } } } } void SPFA2(int s) { memset(vis,false,sizeof(vis)); head = 1,tail = 1; q[1] = s; ma[s] = mon[s]; for(head = 1;head <= tail;++head) { int x = q[head]; vis[x] = false; for(int i = lin2[x],y;i;i = e2[i].next) { if(ma[y = e2[i].y] < ma[x]) { ma[y] = max(mon[y],ma[x]); if(!vis[y]) { vis[y] = true; q[++tail] = y; } } } } } int main() { a = read(); b = read(); for(int i = 1;i <= a;++i) mon[i] = read(); for(int i = 1;i <= b;++i) { int x,y,z; x = read(); y = read(); z = read(); add1(x,y); add2(y,x); if(z == 2) { add1(y,x); add2(x,y); } } SPFA1(1); SPFA2(a); for(int i = 1;i <= a;++i) ans = max(ans,ma[i] - mi[i]); write(ans); return 0; }
下面是第一次的代码,大佬可以参考一下......
#include<bits/stdc++.h> #define INF 0x7f7f7f7f #define MAXN 100005 using namespace std; vector<int> g[MAXN]; int n,m,f[MAXN],mi[MAXN],c[MAXN]; void dfs(int x,int minx,int pre) { int flag=1; minx=min(c[x],minx); if (mi[x]>minx) mi[x]=minx,flag=0; int maxx=max(f[pre],c[x]-minx); if (f[x]<maxx) f[x]=maxx,flag=0; if (flag) return; for (int i=0;i<g[x].size();i++) dfs(g[x][i],minx,x); } int main() { scanf("%d%d",&n,&m); for (int i=0;i<MAXN;i++) mi[i]=INF; for (int i=1;i<=n;i++) scanf("%d",&c[i]); for (int i=1;i<=m;i++) { int t1,t2,t3; scanf("%d%d%d",&t1,&t2,&t3); g[t1].push_back(t2); if (t3==2) g[t2].push_back(t1); } dfs(1,INF,0); printf("%d\n",f[n]); return 0; }
标签:size nod turn color write std 水过 putchar return
原文地址:https://www.cnblogs.com/AK-ls/p/10175508.html