/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 5ms 6.375 MiB
#2 Accepted 8ms 8.25 MiB
#3 Accepted 13ms 6.625 MiB
#4 Accepted 10ms 10.375 MiB
#5 Accepted 10ms 10.375 MiB
#6 Accepted 186ms 11.215 MiB
#7 Accepted 225ms 11.617 MiB
#8 Accepted 192ms 11.0 MiB
#9 Accepted 191ms 11.34 MiB
#10 Accepted 274ms 11.707 MiB

代码

#include <stdio.h>
#include <iostream>
#include <cstring>
const int maxn = 100005;
const int maxn2 = 1e3+5;
int n,m;
using namespace std;
int head[maxn2],nex[maxn<<1],dis[maxn<<1],we[maxn<<1],q,tag[maxn<<1],tag2[maxn<<1];
double pe[maxn<<1];
inline void connect(int a,int b,int w,double p) {
	nex[++q] = head[a],head[a] =q,dis[q] = b,we[q] = w,pe[q] = p;
	nex[++q] = head[b],head[b] = q,dis[q] = a,we[q] = w,pe[q] = p;
}
int que[10000000],hh,rr,dist[maxn2],used[maxn2][maxn2],father[maxn2],visit[maxn2];
double sum2;
int mindist = 1e9;
void spfa() {
	mindist = 1e9;
	for(int i = 0;i<=n;i++) dist[i] = 1e9;
	que[rr++]  = 1;
	visit[1]  = 1;
	dist[1] = 0;
	while(hh!=rr) {
		int a = que[hh++];
		for(int i = head[a]; i; i = nex[i]) {
			if(tag2[i]) continue;
			int to = dis[i];
			if(dist[to]>dist[a]+we[i]) {
				dist[to] = dist[a] + we[i];
				used[a][to] = i,father[to]  = a;
				if(visit[to]==0) visit[to] = 1,que[rr++] = to;
			}
		}
		visit[a] = 0;
	}
	mindist = dist[n];
}
void dfs(int a) {
	if(father[a]==0) return;
	if(a!=1) {
		int pre= father[a],line = used[pre][a];
		tag[line] = 1,sum2+=pe[line];
		dfs(father[a]);
	}
}
int main() {
	scanf("%d%d",&n,&m);
	double ans = 0;
	double sum = 0;
	for(int i = 1; i<=m; i++) {
		int x,y,z;
		double p;
		cin>>x>>y>>z>>p;
		connect(x,y,z,p);
		sum+=p;
	}
	spfa();
	dfs(n);
	if(mindist==1e9) mindist = 0;
	ans+= (sum-sum2)*mindist;
	ans+=(1-sum)*mindist;
	for(int i = 1;i<=m;i++){
		int i1 = i*2-1,i2 = i*2;
		if(tag[i1]||tag[i2]){
			tag2[i1] = tag2[i2] = 1;
			spfa();
			if(mindist == 1e9) mindist = 0;
			ans+= pe[i1]*mindist;
			tag2[i1] = tag2[i2] = 0;
		}
	}
	printf("%.7f",ans);
	return 0;
}

信息

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