poj 1125 Stockbroker Grapevine (dij优化 0ms)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 110
#define pa pair<int,int>
using namespace std;
int n,num,head[maxn],dis[maxn],ans,len=0x7fffffff,f[maxn];
struct node
{
int u,v,t,pre;
}e[maxn*maxn];
void Add(int from,int to,int dis)
{
num++;
e[num].v=to;
e[num].u=from;
e[num].t=dis;
e[num].pre=head[from];
head[from]=num;
}
void Dij(int s)
{
priority_queue<pa,vector<pa>,greater<pa> >q;
q.push(make_pair(,s));
dis[s]=;
while(!q.empty())
{
int k=q.top().second;
q.pop();
if(f[k])continue;
f[k]=;
for(int i=head[k];i;i=e[i].pre)
if(dis[e[i].v]>dis[k]+e[i].t)
{
dis[e[i].v]=dis[k]+e[i].t;
q.push(make_pair(dis[e[i].v],e[i].v));
}
}
}
int main()
{
while()
{
scanf("%d",&n);
if(n==)break;
num=;ans=;len=0x7fffffff;
memset(head,,sizeof(head));
int x,y,z;
for(int i=;i<=n;i++)
{
scanf("%d%",&x);
for(int j=;j<=x;j++)
{
scanf("%d%d",&y,&z);
Add(i,y,z);
}
}
for(int i=;i<=n;i++)
{
memset(dis,/,sizeof(dis));
memset(f,,sizeof(f));
Dij(i);
int falg=,maxx=;
for(int j=;j<=n;j++)
{
if(dis[i]>)falg=;
maxx=max(maxx,dis[j]);
}
if(falg==)continue;
else
{
if(len>maxx)
{
len=maxx;
ans=i;
}
}
}
if(ans)printf("%d %d\n",ans,len);
else printf("disjoint\n");
}
}
上一篇:cf 460 E. Congruence Equation 数学题


下一篇:Rsync常见错误和问题