-
1847 Tram (1)
-
3259 Wormholes (3)
-
3169 Layout (4)
-
2253 Frogger (4)
-
3255 Roadblocks (5)
-
1556 The Doors (5)
-
2502 Subway (6)
-
3463 Sightseeing (6)
-
2135 Farm Tour (7)
-
2404 Jogging Trails (9, challenge problem)
poj3169
spfa做差分约束
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; #define maxn 10010 #define inf 0x3f3f3f3f int d[maxn],cnt[maxn]; bool vis[maxn]; queue<int> q; typedef pair<int,int> pii; vector <pii> adj[maxn]; int main(){ // freopen("layout.6.in","r",stdin); int n,a,b,i,j,k,u,v,ok,c; vector<pii>::iterator it; scanf("%d%d%d",&n,&a,&b); d[1]=0; for(i=2;i<=n;++i){ d[i]=inf; adj[i].push_back(make_pair(i-1,0)); } while(a--){ scanf("%d%d%d",&u,&v,&c); adj[u].push_back(make_pair(v,c)); } while(b--){ scanf("%d%d%d",&u,&v,&c); adj[v].push_back(make_pair(u,-c)); } ok=0; for(i=1;i<n;++i){ q.push(i); vis[i]=cnt[i]=1; } while(!q.empty()){ u=q.front(); vis[u]=0; q.pop(); // printf("u=%d\n",u); for(it=adj[u].begin();it!=adj[u].end();++it){ v=(*it).first; c=(*it).second; // printf("v=%d\n",v); if(d[v]>d[u]+c){ d[v]=d[u]+c; if(!vis[v]){ if(++cnt[v]>=n) { ok=1; goto L; } vis[v]=1; // printf("%d ",v); q.push(v); } } } } L: if(d[n]==inf){ printf("-2\n"); return 0; } for(u=1;u<=n;++u){ for(it=adj[u].begin();it!=adj[u].end();++it){ v=(*it).first; c=(*it).second; if(d[v]>d[u]+c){ // printf("u=%d v=%d c=%d du=%d dv=%d\n",u,v,c,d[u],d[v]); ok=1; goto G; } } } G: if(ok){ printf("-1\n"); }else{ printf("%d\n",d[n]); } return 0; }
poj3255A*+dijkstra求次短路(k=2短路)
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<iostream> using namespace std; #define inf 0x3f3f3f3f #define maxn 5010 typedef pair<int,int> pii; int d[maxn]; vector <pii> adjz[maxn]; vector <pii> adjf[maxn]; priority_queue<pii,vector<pii>,greater<pii> > pq; bool vis[maxn]; int cnt[maxn]; int n,m,s,t,k; typedef struct S{ int v,c; S(){} S(int cc,int vv):c(cc),v(vv){} bool operator < (const struct S& tt) const{ return c+d[v]>tt.c+d[tt.v]; } }NODE; priority_queue<NODE> q; void init(){ int i; for(i=1;i<=n;++i) { d[i]=inf; adjz[i].clear(); adjf[i].clear(); } memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); while(!pq.empty()) pq.pop(); while(!q.empty()) q.pop(); } void dijkstra(){ int u,v,c; d[t]=0; pq.push(make_pair(0,t)); vector<pii>::iterator it; while(!pq.empty()){ u=pq.top().second; pq.pop(); if(vis[u]) continue; vis[u]=1; for(it=adjf[u].begin();it!=adjf[u].end();++it){ v=(*it).second; if(vis[v]) continue; c=(*it).first; if(d[v]>d[u]+c){ d[v]=d[u]+c; pq.push(make_pair(d[v],v)); } } } } int Astar(){ int u,v,c,dc; if(d[s]==inf) return -1; q.push(NODE(0,s)); while(!q.empty()){ u=q.top().v; c=q.top().c; q.pop(); ++cnt[u]; if(u==t && cnt[u]==k) return c; if(cnt[u]>k) continue; vector<pii>::iterator it; for(it=adjz[u].begin();it!=adjz[u].end();++it){ v=(*it).second; dc=(*it).first; q.push(NODE(dc+c,v)); } } return -1; } int main(){ int i,j,u,v,c; while(scanf("%d%d",&n,&m)!=EOF){ init(); while(m--){ scanf("%d%d%d",&u,&v,&c); adjz[u].push_back(make_pair(c,v)); adjf[v].push_back(make_pair(c,u)); adjz[v].push_back(make_pair(c,u)); adjf[u].push_back(make_pair(c,v)); } s=1,t=n,k=2; if(s==t) ++k; dijkstra(); // for(i=1;i<=n;++i) printf("di=%d\n",d[i]); printf("%d\n",Astar()); } return 0; }
poj2502dijkstra和spfa的模板
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<iostream> #include<cmath> using namespace std; #define inf 1e10 #define eps 1e-8 #define maxn 210 typedef pair<double,int> pii; double d[maxn]; vector <pii> adj[maxn]; priority_queue<pii,vector<pii>,greater<pii> > pq; bool vis[maxn]; typedef struct S{ double x,y; }NODE; NODE node[maxn]; double dis(int a,int b){ return sqrt((node[a].x-node[b].x)*(node[a].x-node[b].x)+(node[a].y-node[b].y)*(node[a].y-node[b].y)); } int n; void dijkstra(){ int u,v; double c; d[0]=0; for(u=1;u<n;++u) d[u]=inf; pq.push(make_pair(0.0,0)); vector<pii>::iterator it; while(!pq.empty()){ u=pq.top().second; pq.pop(); // if(u==1) return; if(vis[u]) continue; vis[u]=1; for(it=adj[u].begin();it!=adj[u].end();++it){ v=(*it).second; if(vis[v]) continue; c=(*it).first; if(d[v]>d[u]+c){ d[v]=d[u]+c; pq.push(make_pair(d[v],v)); } } } } void spfa(){ int u,v; double c; queue<int> q; q.push(0); vis[0]=1; d[0]=0; for(u=1;u<n;++u) d[u]=inf; vector<pii>::iterator it; while(!q.empty()){ u=q.front(); q.pop(); vis[u]=0; for(it=adj[u].begin();it!=adj[u].end();++it){ v=(*it).second; c=(*it).first; if(d[v]>d[u]+c){ d[v]=d[u]+c; if(!vis[v]) { q.push(v); vis[v]=1; } } } } } bool equal(double x,double y){ if(x-y<eps && y-x<eps) return 1; return 0; } int main(){ // freopen("d.in","r",stdin); int i,j,s,l; double x,y,dc; scanf("%lf%lf%lf%lf",&node[0].x,&node[0].y,&node[1].x,&node[1].y); n=2; s=2; while(scanf("%lf%lf",&x,&y)!=EOF){ if(equal(x,-1)) continue; node[n].x=x; node[n].y=y; ++n; while(scanf("%lf%lf",&x,&y)!=EOF){ if(equal(x,-1)){ for(i=s;i<n-1;++i){ dc=dis(i,i+1)*0.0015; adj[i].push_back(make_pair(dc,i+1)); adj[i+1].push_back(make_pair(dc,i)); } s=n; break; } node[n].x=x; node[n].y=y; ++n; } } for(i=0;i<n;++i){ for(j=i+1;j<n;++j){ dc=dis(i,j)*0.006; adj[i].push_back(make_pair(dc,j)); adj[j].push_back(make_pair(dc,i)); } } // spfa(); dijkstra(); printf("%.0f\n",d[1]); return 0; }
poj1344根据公式: w(x, c) = (M[a, c] +M[b, c] -M[a, b])/2
#include<cstdio> int a[35][35],n; int main(){ int i,j; int ans; while(scanf("%d",&n)!=EOF && n){ for(i=0;i<n-1;++i){ for(j=i+1;j<n;++j){ scanf("%d",&a[i][j]); } } ans=0; ans+=a[0][1]; for(i=2;i<n;++i){ ans+=(a[i-2][i]+a[i-1][i]-a[i-2][i-1])>>1; } printf("%d\n",ans); } return 0; }
poj2135spfa松弛的最大流最小费模板
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; #define maxn 1005 #define inf 0x3f3f3f3f struct NODE{ NODE(){} NODE(int uu,int vv,int cc,int ww,int pp):u(uu),v(vv),c(cc),w(ww),p(pp){} int u,v,c,w,p; }; vector<NODE> adj[maxn],nu; vector<NODE>::iterator pre[maxn]; queue<int> q; int cnt[maxn]; int d[maxn]; bool vis[maxn]; int n; void add(int u,int v,int c,int w){ adj[u].push_back(NODE(u,v,c,w,adj[v].size())); adj[v].push_back(NODE(v,u,0,-w,adj[u].size()-1)); } int spfa(){ while(!q.empty()) q.pop(); fill(d,d+n+2,inf); memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis)); d[0]=0; cnt[0]=1; vis[0]=1; pre[0]=nu.begin(); q.push(0); int u,v,c; vector<NODE>::iterator it; while(!q.empty()){ u=q.front(); q.pop(); vis[u]=0; for(it=adj[u].begin();it!=adj[u].end();++it){ v=it->v; if(it->c && d[v]>d[u]+it->w){ d[v]=d[u]+it->w; pre[v]=it; if(!vis[v]){ if(++cnt[v]>n+1) return 0; vis[v]=1; q.push(v); } } } } if(d[n+1]==inf) return 0; return 1; } int mfmc(){ int ans=0,m; vector<NODE>::iterator it; while(spfa()){ m=inf; for(it=pre[n+1];it!=nu.begin();it=pre[it->u]){ m=min(m,it->c); } for(it=pre[n+1];it!=nu.begin();it=pre[it->u]){ it->c-=m; adj[it->v][it->p].c+=m; } ans+=m*d[n+1]; } return ans; } int main(){ // freopen("d.in","r",stdin); int u,v,w,m; scanf("%d%d",&n,&m); while(m--){ scanf("%d%d%d",&u,&v,&w); add(u,v,1,w); add(v,u,1,w); } add(0,1,2,0); add(n,n+1,2,0); printf("%d\n",mfmc()); return 0; }
Assignment 7: Shortest Path Algorithms
最新推荐文章于 2022-09-03 11:26:35 发布
本文深入探讨了信息技术领域的核心算法及其在不同应用场景中的实践,包括但不限于数据挖掘、机器学习、区块链、人工智能音视频处理等前沿技术。文章旨在为读者提供全面的技术知识,帮助其在复杂的信息技术环境中做出明智决策。
摘要由CSDN通过智能技术生成