POJ - 2387 Til the Cows Come Home
生活随笔
收集整理的這篇文章主要介紹了
POJ - 2387 Til the Cows Come Home
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
感覺一直寫的dij堆優化都是假的,最短路板子spfa+ dij堆優化?
SPFA
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define inff 0x3f3f3f3f
#define lowbit(x) x&(-x)
#define PI 3.14159265358979323846
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define min3(x,y,z) min(min(x,y),min(y,z))
#define pii make_pair
#define pr pair<int,int>
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
typedef long long ll;
const ll inFF=9223372036854775807;
typedef unsigned long long ull;
using namespace std;
const int maxn=1e4+5;
int d[maxn],vis[maxn];
int head[maxn],sign;
int n,m;
struct node
{int to,p,val;
}edge[maxn];
void add(int u,int v,int val)
{edge[sign]=node{v,head[u],val};head[u]=sign++;
}
void init()
{sign=0;memset(head,-1,sizeof(head));
}
void spfa()
{for(int i=1;i<=n;i++) d[i]=inff;d[n]=0;queue<int> q;vis[n]=1;q.push(n);while(!q.empty()){int u=q.front();q.pop(),vis[u]=0;for(int i=head[u];~i;i=edge[i].p){int v=edge[i].to;if(d[v]>d[u]+edge[i].val){d[v]=d[u]+edge[i].val;if(!vis[v]){q.push(v);vis[v]=1;}}}}
}
int main()
{int x,y,z;while(cin>>m>>n){init();for(int i=1;i<=m;i++){scanf("%d %d %d",&x,&y,&z);add(x,y,z),add(y,x,z);}spfa();printf("%d\n",d[1]);}return 0;
}
DIJ+優先隊列優化
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define inff 0x3f3f3f3f
#define lowbit(x) x&(-x)
#define PI 3.14159265358979323846
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define min3(x,y,z) min(min(x,y),min(y,z))
#define pii make_pair
#define pr pair<int,int>
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
typedef long long ll;
const ll inFF=9223372036854775807;
typedef unsigned long long ull;
using namespace std;
const int maxn=1e4+5;
int d[maxn];
int head[maxn],sign;
int n,m;
struct node
{int to,p,val;
}edge[maxn];
void add(int u,int v,int val)
{edge[sign]=node{v,head[u],val};head[u]=sign++;
}
struct nod
{int u,val;bool friend operator<(nod s,nod e){return s.val>e.val;}
};
void init()
{sign=0;memset(head,-1,sizeof(head));
}
void dij()
{for(int i=1;i<=n;i++) d[i]=inff;d[n]=0;priority_queue<nod> q;q.push(nod{n,0});while(!q.empty()){nod now=q.top();q.pop();if(d[now.u]<now.val) continue;for(int i=head[now.u];~i;i=edge[i].p){int v=edge[i].to;if(d[v]>d[now.u]+edge[i].val){d[v]=d[now.u]+edge[i].val;q.push(nod{v,d[v]});}}}
}
int main()
{int x,y,z;while(cin>>m>>n){init();for(int i=1;i<=m;i++){scanf("%d %d %d",&x,&y,&z);add(x,y,z),add(y,x,z);}dij();printf("%d\n",d[1]);}return 0;
}
?
總結
以上是生活随笔為你收集整理的POJ - 2387 Til the Cows Come Home的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ2038 小Z的袜子(hose)
- 下一篇: 奶牛排序 cow sort 置换群