标签:
1 5 7 5 2 100 3 5 80 2 3 70 2 1 50 3 4 90 4 1 85 3 1 70
61.200000
原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=1274
1w个点,5w条边,邻接矩阵MLE,那就链式向前星建图吧,既然用了链式向前星,那就用spfa吧。
由于刚开始用Dijkstra算法MLE,后改用spfa,MAXN没改,提交了TLE。。ORZ
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50000+5;
int n,m;
double dis[maxn];
bool vis[maxn];
struct Node
{
int to,next;
double w;
} edge[maxn<<1];//<<1 TLE
int cnt;
int head[maxn];
void Init()
{
cnt=0;
for(int i=1; i<=n; i++)
{
head[i]=-1;
vis[i]=false;
dis[i]=0;
}
}
void addEdge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w/100.0;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void spfa()
{
queue<int>q;
q.push(1);
vis[1]=true;
dis[1]=1;
while(!q.empty())
{
int p = q.front();
q.pop();
vis[p]=false;
for(int i=head[p];~i; i=edge[i].next)
{
int to=edge[i].to;
double w=edge[i].w;
if(dis[to]<dis[p]*w)
{
dis[to]=dis[p]*w;
if(!vis[to])
{
q.push(to);
vis[to]=true;
}
}
}
}
printf("%.6f\n",dis[n]*100);
}
int main()
{
int T;
//freopen("data/1274.txt","r",stdin);
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
Init();
int x,y,z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
addEdge(x,y,z);
addEdge(y,x,z);
}
spfa();
}
return 0;
}
NYOJ 1274 信道安全【最短路,spfa+链式向前星】
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/52260910