列表

详情


NC206685. AcrosstheFirewall

描述

"Across the Great Wall, we can reach every corner in the world." This is the first e-mail sent from China.

However, sending emails in the dark organization is not so easy. Networks are controlled by the firewalls. To protect the secret of APTX4869, A few firewall rules are set in the local area network.

Bourbon wants to destroy the organization. He cracked into the LAN and want to download documents about APTX4869. He knows the rules of firewall, but he can't stop the firewall.

There are n computers and m channels in the network. Files can be transferred only in the channels, whose rate is limited. For each channel i, 3 arguments u_i, v_i, w_i are set in the rules, which means the file transfer speed between computer u_i and v_i is w_i KB/s. Data transfer is bidirectional. Computers are also slow. For each computer i, only a_i KB data transfer can be done in one second. Network delay is not considered.

Now Bourbon has controlled the k-th computer and started to download the document from computer 1. It is guaranteed that there is one channel connecting 1 and k. The size of this secret document is s KB. He wants to know how much time it takes at least to complete this mission. Now, this clever man, can you help Bourbon?

输入描述

First line contains two integers n and .

The second line contains n integers for the computers.

Next m lines each contains three integers for the rules.

The last line of input contains two integers k and .

输出描述

Output a number for the answer with an absolute or relative error of at most . Precisely speaking, assume that your answer is a and and the jury's answer is b, your answer will be considered correct if , where  means the maximum of x and y and |x| means the absolute value of x.

示例1

输入:

4 5
2097152 1048576 1048576 1048576
1 2 524288
2 4 524288
1 3 524288
3 4 524288
1 4 1048576
4 262144

输出:

0.25000000

说明:

In this example, the download speed is up to 1048576KB/s, so the time is 0.25s.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 632K, 提交时间: 2020-06-13 17:12:34

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1010;
const int mod=1000000007;
char s[N];
ll a[N];
ll ans[N];
ll b[N];
ll net[N][N];
ll use[N][N];
vector<pair<ll,ll>>v[N];
bool vis[256];
ll mn,flag;
void dfs(int x) {
    vis[x]=1;
    if(x==1) {
        flag=1;
        return ;
    }
    for(auto &i:v[x]) {
        if(vis[i.first]) continue;
        if(a[i.first]-b[i.first]==0||i.second-use[i.first][x]==0) continue;
        ll tmp=mn;
        mn=min(min(mn,i.second-use[i.first][x]),a[i.first]-b[i.first]);
        dfs(i.first);
        if(flag) {
            b[i.first]+=mn;
            use[i.first][x]+=mn;
            use[x][i.first]+=mn;
            break;
        }
        mn=tmp;
    }
}
int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    int n,m,t,k,s;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=m;i++) {
        int x,y,w;
        cin>>x>>y>>w;
        v[x].push_back({y,w});
        v[y].push_back({x,w});
    }
    cin>>k>>s;
    flag=1;
    while(flag) {
        flag=0;
        memset(vis,0,sizeof vis);
        mn=a[k]-b[k];
        if(mn==0) break;
        dfs(k);
        b[k]+=mn;
    }
    printf("%.10f\n",s*1.0/b[k]);
    return 0;
}

C++14(g++5.4) 解法, 执行用时: 5ms, 内存消耗: 492K, 提交时间: 2020-06-15 23:21:27

#include<bits/stdc++.h>
using namespace std;
const int INF=1e9;
int n,m,s,ans,dep[405],que[405];
struct edge{
	int w,to,rev;
};
vector<edge> e[405];
bool bfs(){
	int i,x,sz,head=0,tail=1;
	memset(dep,0,sizeof(dep));
	dep[1]=que[1]=1;
	while (head^tail){
		x=que[++head];
		for (i=0,sz=e[x].size();i<sz;i++)
		if (!dep[e[x][i].to]&&e[x][i].w){
			dep[e[x][i].to]=dep[x]+1;
			que[++tail]=e[x][i].to;
		}
	}
	return dep[n+m];
}
int dfs(int x,int flow){
	int i,z,sz,res=0;
	if (!flow||x==n+m) return flow;
	for (i=0,sz=e[x].size();i<sz;i++)
	if (dep[e[x][i].to]>dep[x]&&e[x][i].w){
		z=dfs(e[x][i].to,min(flow,e[x][i].w));
		e[x][i].w-=z;
		e[e[x][i].to][e[x][i].rev].w+=z;
		flow-=z;
		res+=z;
		if (!flow) return res;
	}
	dep[x]=0;
	return res;
}
void dinic(){
	ans=0;
	while (bfs()) ans+=dfs(1,INF);
}
int main(){
	int i,x,y,z;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++){
		scanf("%d",&x);
		e[i].push_back((edge){x,n+i,e[n+i].size()});
		e[n+i].push_back((edge){0,i,e[i].size()-1});
	}
	while (m--){
		scanf("%d%d%d",&x,&y,&z);
		e[x].push_back((edge){z,y,e[y].size()});
		e[y].push_back((edge){0,x,e[x].size()-1});
	}
	scanf("%d%d",&m,&s);
	dinic();
	printf("%.8f",1.0*s/ans);
	return 0;
}

上一题