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

Re:100题纪念!

Posted by luoxuqing at 2016-09-16 16:22:36 on Problem 2607
In Reply To:100题纪念! Posted by:luoxuqing at 2016-09-16 16:22:30
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

const int N=501, inf=1000000000;
struct edge
{
  int v,c,nx;
}e[500010];
int dis[N],res[N],fire[N],head[N],q[N*10],n,m,id,i,j,a,b,w,Min,mx,ans;

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

void spfa(int src, int dis[])
{
  int i,l,r,u,v; 
  struct edge tmp;
  dis[src]=0; l=0; r=1; q[1]=src;
  while (l<r)
  {
    l++; u=q[l];
    i=head[u];
    while (i!=-1)
    {
      v=e[i].v;
      if (dis[u]+e[i].c<dis[v])
      {
        dis[v]=dis[u]+e[i].c;
        r++; q[r]=v;
      }
      i=e[i].nx;
    }
  }
}

int main()
{
  ans=1; Min=inf;
  memset(head,-1,sizeof(head));
  scanf("%d%d",&m,&n);
  for (i=1; i<=n; i++) dis[i]=inf;
  for (i=1; i<=m; i++) scanf("%d",&fire[i]);
  while (scanf("%d",&a)==1)
  {
    scanf("%d%d",&b,&w);
    add(a,b,w); add(b,a,w);
  }
  for (i=1; i<=m; i++)
  {
    if (dis[fire[i]]==0) continue;
    spfa(fire[i],dis);
  }
  for (i=1; i<=n; i++)
  {
    if (dis[i]==0) continue;
    for (j=1; j<=n; j++) res[j]=dis[j];
    spfa(i,res);
    mx=-1;
    for (j=1; j<=n; j++)
      if (res[j]>mx) mx=res[j];
    if (mx<Min){ Min=mx; ans=i;}
  }
  cout<<ans<<endl;
  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