| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
1A spfa 300ms....附代码....前排大佬都是用矩阵转置的....有点烦..../*
这题是一个求最短路的题,有很多牛要参加舞会,我们要计算的是他们每头牛去舞会的路程+回家的路程最短路的和 ,最大的那头牛;那么题意就是求这个,我们的思路是
spfa 求每头牛去的距离,跟回去的距离,加起来,维护一个ans变量,最后输出,回去的路程可能跟去的路程不同,因为在舞会中,奶牛们可能会从其他奶牛哪里得知小路哦!!...然后回家就会很快呢
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
const int Maxn=1005;
const int INF=1e9+7;
using namespace std;
int vis[Maxn],dis[Maxn],head[Maxn],cnt[Maxn],e;
int dis1[Maxn];
int n,m,x;
queue<int>que;
struct Edge
{
int start,end,next,w;
}ed[100001];
void ini()
{
memset(head,-1,sizeof(head));
e=0;
while(!que.empty())que.pop();
}
void addedge(int u,int v,int c)
{
ed[e].start=u;
ed[e].end=v;
ed[e].next=head[u];
ed[e].w=c;
head[u]=e++;
}
bool spfa(int s) //bool是为了防止环路,但是这题没用到
{
for(int i=1;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
cnt[i]=0;
}
dis[s]=0;
vis[s]=1;
que.push(s);
while(!que.empty())
{
int temp=que.front();
que.pop();
vis[temp]=0;
for(int i=head[temp];i!=-1;i=ed[i].next)
{
int x=ed[i].start;
int y=ed[i].end;
int w=ed[i].w;
if(dis[y]>dis[x]+w)
{
dis[y]=dis[x]+w;
if(!vis[y])
{
vis[y]=1;
if(++cnt[y]>n)
{
return true;
}
que.push(y);
}
}
}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
ini();
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
int ans=-1;
spfa(x);
for(int i=1;i<=n;i++)
{
dis1[i]=dis[i]; //求出x点到各点的距离
}
for(int i=1;i<=n;i++)
{
int temp=0;
if(i==x)continue;
spfa(i);
temp+=dis[x]; //加上的是去x的距离
temp+=dis1[i]; //加上的是回家的距离
ans=max(ans,temp); //判断奶牛来回的最大距离
}
printf("%d\n",ans);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator