/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 6ms 4.25 MiB
#2 Accepted 7ms 2.375 MiB
#3 Accepted 12ms 4.359 MiB
#4 Accepted 8ms 2.25 MiB
#5 Accepted 13ms 2.344 MiB
#6 Accepted 221ms 5.0 MiB
#7 Accepted 198ms 4.277 MiB
#8 Accepted 173ms 6.25 MiB
#9 Accepted 172ms 6.25 MiB
#10 Accepted 255ms 6.0 MiB

代码

#include<bits/stdc++.h>
#define maxn 1005
#define maxm 200005
using namespace std;
struct edge
{
	int to,next,len;
	double p;
}a[maxm];
int pre[maxn];
int n,m,tot;
int v[maxn];
int first[maxn];
int exist[maxm];
int dis[maxn];
int tt[maxn];
int que[500000];
inline const void read(int &a)
{
	a=0;
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c<='9'&&c>='0')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar(); 
	}
} 
void add_edge(int x,int y,int z,double p)
{
	a[++tot].to=y;
	a[tot].len=z;
	a[tot].p=p;
	a[tot].next=first[x];
	first[x]=tot;
}
int spfa(int xx)
{
	memset(dis,0x7f,sizeof(dis));
	memset(v,0,sizeof(v));
	dis[1]=0;
	que[1]=1;
	int front=0,rail=1;
	v[1]=1;
	while(front<rail)
	{
		int x=que[++front];
		for(int i=first[x];i!=-1;i=a[i].next)
		{
			int vv=a[i].to;
			//cout<<"vv="<<vv<<' ';
			if(exist[i]&&dis[x]+a[i].len<dis[vv])
			{
				//cout<<"vv="<<vv<<endl;
				dis[vv]=dis[x]+a[i].len;
				if(xx==0)
				//cout<<"dis["<<vv<<"]="<<dis[vv]<<endl;
				pre[vv]=i;
				if(!v[vv])
				{
					v[vv]=1;
					que[++rail]=vv;
				}
			}
		}
		v[x]=0;
	}
	//cout<<"\ndis["<<n<<"]="<<dis[n]<<endl;
	if(dis[n]<dis[0])
	return dis[n];
	else return 0;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int x1,x2,x3;
	double y;
	read(n);read(m);
	memset(first,-1,sizeof(first));
	memset(exist,1,sizeof(exist));
	memset(pre,-1,sizeof(pre));
	memset(tt,-1,sizeof(tt));
	for(int i=1;i<=m;i++)
	{
		read(x1);read(x2);read(x3);
		cin>>y;
		add_edge(x1,x2,x3,y);
		add_edge(x2,x1,x3,y);
	}
	int k=spfa(0);
	double temp=0;
	if(k==0)
	{
		cout<<"0.0000000"<<endl;
		return 0;
	}
	//for(int i=2;i<=n;i++)
	//cout<<a[pre[i]].to<<endl;
	//cout<<"k="<<k<<endl;
	tt[0]=0;
	for(int i=2;i<=n;i++)
	{
		if(pre[i]!=-1)
		{
			//cout<<"p.pre["<<i<<"]="<<a[pre[i]].p<<endl;
			tt[++tt[0]]=pre[i];
			temp+=a[pre[i]].p;
			//cout<<"temp="<<temp<<endl;
		}
	}
	double ans=(double)k*(1-temp);
	//cout<<"ans="<<ans<<" t[0]="<<tt[0]<<endl;
	for(int i=1;i<=tt[0];i++)
	{
		if(a[tt[i]].p>0&&tt[i]!=-1)
		{
			int w=1;
			if(tt[i]%2==0)
			w=-1;
			exist[tt[i]]=0;exist[tt[i]+w]=0;
			int k=spfa(1);
			//cout<<"ban:"<<tt[i]<<' '<<tt[i]+w<<endl;
			//cout<<"k="<<k<<endl;
			ans+=(double)k*a[tt[i]].p;
			exist[tt[i]]=1;exist[tt[i]+w]=1;
			//cout<<"ans="<<ans<<endl;
		}
	}
	printf("%.7f\n",ans);
	return 0;
}

信息

递交者
类型
递交
题目
从中国到阿布拉斯基索沃尔朗特乎斯德麦尔维(原创)
题目数据
下载
语言
C++
递交时间
2017-11-07 14:23:20
评测时间
2017-11-07 14:23:20
评测机
分数
100
总耗时
1070ms
峰值内存
6.25 MiB