Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

spfa 1A 水之!!附代码!!

Posted by wocha at 2012-08-21 10:12:16 on Problem 2387
#include <queue>
#include <cstdio>
#include <iostream>
#define M 101000
#define inf 0x3f3f3f3f
using namespace std;
int start[M],e,end[M],next[M],cost[M];
inline void Init(){
e=0;  memset(start,-1,sizeof(start));
}
inline void add(int u2,int v2,int w2){
end[e]=v2;
cost[e]=w2;
next[e]=start[u2];
start[u2]=e++;
}
int vis[M],cnt[M],d[M];
void spfa (int st,int N){
memset(vis,0,N+2);
memset(cnt,0,N+2);
memset(d,inf,sizeof(d));
queue<int > Q; int u1,v1, w1;
Q.push(st), vis[st]=1, cnt[st]++, d[st]=0;
while(!Q.empty()){
u1=Q.front();Q.pop();vis[u1]=0;
for(int i=start[u1];i!=-1;i=next[i]){
v1=end[i];  w1=cost[i];
if(d[v1]>d[u1]+w1){
d[v1]=d[u1]+w1;
if(!vis[v1]){vis[v1]=1;
Q.push(v1);
if(++cnt[v1]>N) return;
}
}
}
}
}
	int main(){
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m)){
Init();
for(int i=0;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa(m,n);
printf("%d\n",d[1]) ;
}                                                                      
}                                  

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator