用来练习邻接表结构 poj3268 pku spfa
来源:互联网 发布:淘宝有电脑版客户端吗 编辑:程序博客网 时间:2024/06/05 23:06
View Code
#include<stdio.h>
#include<string.h>
#define MAXN 101000
#define INF 2000000
struct NODE
{
int v;
int w;
NODE *next;
}edge[MAXN],redge[MAXN],temp[MAXN*2];
int pos=0;
int ecost[MAXN];
int N,M,W,s,Q[MAXN];
bool vis[MAXN];
void spfa(int dir)//0为正向,1为反向
{
int head,rear,u,i;
NODE*ptr;
head=0,rear=1;
memset(vis,0,sizeof(vis));
for(i=0;i<=N;i++)
ecost[i]=INF;
Q[0]=s;
ecost[s]=0;
while(head!=rear)
{
u=Q[head];
head++;
vis[u]=false;//出队列
if(dir!=0) ptr=edge[u].next;
else ptr=redge[u].next;
while(ptr)
{
if(ecost[ptr->v]>ecost[u]+ptr->w)
{
ecost[ptr->v]=ecost[u]+ptr->w;
if(!vis[ptr->v])
{
Q[rear]=ptr->v;
rear++;
vis[ptr->v]=true;
}
}
ptr=ptr->next;
}
}
}
void addedge(int x,int y,int w)
{
NODE *ptr=&temp[pos++];
ptr->v=y;
ptr->w=w;
ptr->next=edge[x].next;
edge[x].next=ptr;
ptr=&temp[pos++];
ptr->v=x;
ptr->w=w;
ptr->next=redge[y].next;
redge[y].next=ptr;
}
int ans[MAXN],maxtime;
int main()
{
int i,x,y,w;
while(scanf("%d%d%d",&N,&M,&s)!=EOF)
{
pos=0;
for(i=0;i<=N;i++)
{
edge[i].next=NULL;
redge[i].next=NULL;
}
for(i=0;i<M;i++)
{
scanf("%d%d%d",&x,&y,&w);
addedge(x,y,w);
}
maxtime=0;
memset(ans,0,sizeof(ans));
spfa(0);
for(i=1;i<=N;i++)
{
if(i!=s) ans[i]+=ecost[i];
}
spfa(1);
for(i=1;i<=N;i++)
{
if(i!=s)
{
ans[i]+=ecost[i];
if(ans[i]>maxtime)
maxtime=ans[i];
}
}
printf("%d\n",maxtime);
}
return 0;
}
- 用来练习邻接表结构 poj3268 pku spfa
- PKU 1511 Invitation Cards (SPFA+邻接表)
- pku 1511(最短路径,spfa静态邻接表)
- SDUTOJ 2143 图结构练习——最短路径 邻接链表 SPFA算法
- poj3268---spfa
- POJ3268 SPFA
- poj3268 spfa
- spfa+邻接表
- SPFA 邻接表
- SPFA+邻接表
- 邻接表的spfa
- cug1697 spfa+邻接表
- [模板]spfa+邻接表
- SPFA 邻接表模式
- poj3268俩次SPFA。。。。。
- SPFA + 静态邻接表 模板
- POJ 1511 邻接表+spfa
- HDU-2544(SPFA()邻接表)
- hdu 4034 graph
- hdu 4033
- pku 1061
- poj pku2263floyd
- 约束的个数
- 用来练习邻接表结构 poj3268 pku spfa
- pku 3660
- 树形DP pku1947
- tubao pku1113
- poj 2481 cows 树状数组 老少皆宜版解答
- hdu 4000 树状数组
- hdu 4006 求第k大的数
- hdu 4046 树状数组
- hdu 3694 费马点的应用