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

第100题纪念,附AC代码

Posted by lyh0313 at 2018-08-18 11:52:17 on Problem 1511
#include<cstdio>//十足的水题
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
#include<queue>
#include<iomanip>
#include<algorithm>
using namespace std;
const int N=1000010;

struct edge
{
	int v,nx,w;
}ee[N],e[N];
int n,m,headd[N],head[N],id;
int l,r,q[2*N],d[N];
bool vis[N];

void add(int u,int v,int w)
{
	id++;
	e[id].v=v;e[id].w=w;e[id].nx=head[u];head[u]=id;
	ee[id].v=u;ee[id].w=w;ee[id].nx=headd[v];headd[v]=id;
}

long long spfaa()
{
	int i,j,u,v;
	memset(d,0x7f,sizeof(d));
	memset(vis,false,sizeof(vis));
	l=1;r=1;q[1]=1;d[1]=0;vis[1]=true;
	while(l<=r)
	{
		u=q[l];l++;
		vis[u]=false;
		for(i=head[u];i!=-1;i=e[i].nx)
		{
			v=e[i].v;
			if(d[u]+e[i].w<d[v])
			{
				d[v]=d[u]+e[i].w;
				if(!vis[v])
				{
					r++;q[r]=v;
				}
			}
		}
	}
	long long res=0;
	for(i=2;i<=n;i++) res+=d[i];
	return res;
}

long long spfab()
{
	int i,j,u,v;
	memset(d,0x7f,sizeof(d));
	memset(vis,false,sizeof(vis));
	l=1;r=1;q[1]=1;d[1]=0;vis[1]=true;
	while(l<=r)
	{
		u=q[l];l++;
		vis[u]=false;
		for(i=headd[u];i!=-1;i=ee[i].nx)
		{
			v=ee[i].v;
			if(d[u]+ee[i].w<d[v])
			{
				d[v]=d[u]+ee[i].w;
				if(!vis[v])
				{
					r++;q[r]=v;
				}
			}
		}
	}
	long long res=0;
	for(i=2;i<=n;i++) res+=d[i];
	return res;
}

int main()
{
	int i,j,cas,a,b,c;
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d%d",&n,&m);
		id=0;
		memset(head,-1,sizeof(head));
		memset(headd,-1,sizeof(headd));
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);
		}
		printf("%I64d\n",spfaa()+spfab());
	}
	return 0;
}

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