G - Island Transport(双向边最大流)

In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Input
  The first line contains one integer T (1<=T<=20), the number of test cases.
  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
Output
  For each test case, output an integer in one line, the transport capacity.
  题面是一道裸的最大流,给出n个点和m条双向边,点以坐标表示,横坐标最小的是源点,最大的是汇点。按照题目建边跑最大流即可,不过题目里给出的是双向边,所以在建立反向边是要注意容量不是0,而是和正向边一样,然后跑最大流。
  不过要注意普通dinic算法会超时,所以这里用的ISAP

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
const int maxm=1e5+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=w;
	e[ne].next=head[v];
	head[v]=ne++;
}
void bfs(int t)
{
	while(!q.empty()) q.pop();
	for(int i=1;i<=n;i++) cur[i]=head[i];
	for(int i=1;i<=n;i++) dep[i]=n;
	dep[t]=0;
	q.push(t);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(dep[e[i].to]==n&&e[i^1].cost)
			{
				dep[e[i].to]=dep[u]+1;
				q.push(e[i].to);
			}
		}
	}
}
int addflow(int s,int t)
{
	int ans=INF,u=t;
	while(u!=s)
	{
		ans=min(ans,e[pre[u]].cost);
		u=e[pre[u]^1].to;
	}
	u=t;
	while(u!=s)
	{
		e[pre[u]].cost-=ans;
		e[pre[u]^1].cost+=ans; 
		u=e[pre[u]^1].to;
	}
	return ans;
}
void ISAP(int s,int t)
{
	int u=s;
	bfs(t);
	for(int i=1;i<=n;i++) num[dep[i]]++;
	while(dep[s]<n)
	{
		if(u==t) 
		{
			maxflow+=addflow(s,t);
			u=s;
		}
		int flag=0;
		for(int &i=cur[u];i!=-1;i=e[i].next)
		{
			if(dep[u]==dep[e[i].to]+1&&e[i].cost)
			{
				flag=1;
				pre[e[i].to]=i;
				u=e[i].to;
				break;
			}
		}
		if(!flag)
		{
			int minn=n-1;
			for(int i=head[u];i!=-1;i=e[i].next)
			{
				if(e[i].cost)
				{
					minn=min(minn,dep[e[i].to]);
				}
			}
			if((--num[dep[u]])==0) break;
			dep[u]=minn+1;
			num[dep[u]]++;
			cur[u]=head[u];
			if(u!=s) u=e[pre[u]^1].to;
		}
	}
}
void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	memset(num,0,sizeof(num));
	maxflow=0;
}
struct island
{
	int x,y;
}is[maxm];
int main()
{
	int tt; 
	scanf("%d",&tt);
	while(tt--)
	{
		scanf("%d%d",&n,&m);
		init();
		S=0;
		T=n+1;
		int minn=1e5+5,maxx=-minn;
		for(int i=1;i<=n;i++)
		{
			int x,y;
			scanf("%d%d",&is[i].x,&is[i].y);
			if(minn>is[i].x)
			{
				minn=is[i].x;
				S=i;
			}
			if(maxx<is[i].x)
			{
				maxx=is[i].x;
				T=i;
			}
		}
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
		}
		ISAP(S,T);
		printf("%d\n",maxflow);
	}
}

另外用最高标号预流推进还能再快很多

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using std::min;
using std::vector;
using std::queue;
using std::priority_queue;
const int N=2e5+5,M=2e5+5,inf=0x3f3f3f3f;
int n,s,t,tot;
int v[M<<1],w[M<<1],first[N],next[M<<1];
int h[N],e[N],gap[N<<1],inq[N];//节点高度是可以到达2n-1的
void init()
{
	tot=0;
	memset(first,0,sizeof(first));
	memset(h,0,sizeof(h));
	memset(e,0,sizeof(e));
	memset(gap,0,sizeof(gap));
	memset(inq,0,sizeof(inq));
}
struct cmp
{
    inline bool operator()(int a,int b) const
    {
        return h[a]<h[b];//因为在优先队列中的节点高度不会改变,所以可以直接比较
    }
};
queue<int> Q;
priority_queue<int,vector<int>,cmp> pQ;
inline void add_edge(int from,int to,int flow)
{
    tot+=2;
    v[tot+1]=from;v[tot]=to;w[tot]=flow;w[tot+1]=flow;
    next[tot]=first[from];first[from]=tot;
    next[tot+1]=first[to];first[to]=tot+1;
    return;
}
inline bool bfs()
{
    int now;
    register int go;
    memset(h+1,0x3f,sizeof(int)*n);
    h[t]=0;Q.push(t);
    while(!Q.empty())
    {
        now=Q.front();Q.pop();
        for(go=first[now];go;go=next[go])
            if(w[go^1]&&h[v[go]]>h[now]+1)
                h[v[go]]=h[now]+1,Q.push(v[go]);
    }
    return h[s]!=inf;
}
inline void push(int now)//推送
{
    int d;
    register int go;
    for(go=first[now];go;go=next[go])
        if(w[go]&&h[v[go]]+1==h[now])
        {
            d=min(e[now],w[go]);
            w[go]-=d;w[go^1]+=d;e[now]-=d;e[v[go]]+=d;
            if(v[go]!=s&&v[go]!=t&&!inq[v[go]])
                pQ.push(v[go]),inq[v[go]]=1;
            if(!e[now])//已经推送完毕可以直接退出
                break;
        }
    return;
}
inline void relabel(int now)//重贴标签
{
    register int go;
    h[now]=inf;
    for(go=first[now];go;go=next[go])
        if(w[go]&&h[v[go]]+1<h[now])
            h[now]=h[v[go]]+1;
    return;
}
inline int hlpp()
{
    int now,d;
    register int i,go;
    if(!bfs())//s和t不连通
        return 0;
    h[s]=n;
    memset(gap,0,sizeof(int)*(n<<1));
    for(i=1;i<=n;i++)
        if(h[i]<inf)
            ++gap[h[i]];
    for(go=first[s];go;go=next[go])
        if(d=w[go])
        {
            w[go]-=d;w[go^1]+=d;e[s]-=d;e[v[go]]+=d;
            if(v[go]!=s&&v[go]!=t&&!inq[v[go]])
                pQ.push(v[go]),inq[v[go]]=1;
        }
    while(!pQ.empty())
    {
        inq[now=pQ.top()]=0;pQ.pop();push(now);
        if(e[now])
        {
            if(!--gap[h[now]])//gap优化,因为当前节点是最高的所以修改的节点一定不在优先队列中,不必担心修改对优先队列会造成影响
                for(i=1;i<=n;i++)
                    if(i!=s&&i!=t&&h[i]>h[now]&&h[i]<n+1)
                        h[i]=n+1;
            relabel(now);++gap[h[now]];
            pQ.push(now);inq[now]=1;
        }
    }
    return e[t];
}
int m;
struct island
{
	int x,y;
}is[M];
signed main()
{
    int tt; 
	scanf("%d",&tt);
	while(tt--)
	{
		scanf("%d%d",&n,&m);
		init();
		s=0;
		t=n+1;
		int minn=1e5+5,maxx=-minn;
		for(int i=1;i<=n;i++)
		{
			int x,y;
			scanf("%d%d",&is[i].x,&is[i].y);
			if(minn>is[i].x)
			{
				minn=is[i].x;
				s=i;
			}
			if(maxx<is[i].x)
			{
				maxx=is[i].x;
				t=i;
			}
		}
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add_edge(x,y,z);
		}
		printf("%d\n",hlpp());
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Functional Safety Island(功能安全小岛)是一个术语,用于描述在一些特定领域中,需要将功能安全保证独立于其他工作程、系统或模块的一片区域。功能安全指的是在系统或产品中保证安全性能,并防止可能的危险或事故。 在汽车行业中,Functional Safety Island常常指的是车辆的电子控制单元(ECU),如发动机控制模块或刹车控制模块等。这些ECU在车辆上被独立地部署,并且有严格的安全标准和要求。它们负责控制汽车的关键功能,如制动、加速和转弯等,以确保车辆在行驶过程中的安全。 Functional Safety Island在其他领域也有应用。例如,在工业自动化中,控制系统通常将功能安全独立地集成到关键过程中。这样可以确保即使在系统的其他部分出现故障或错误时,安全功能仍然能够正常运行,并防止潜在的事故发生。 Functional Safety Island的概念强调了功能安全的重要性,并提醒我们需要将安全性作为一项独立的设计和控制因素考虑。通过将功能安全独立于其他工作程、系统或模块,可以提高系统的可靠性和安全性,减少潜在风险,并保护人们的生命和财产安全。 ### 回答2: Functional Safety Island 是指一个系统中的一个功能安全领域。在汽车、航空、医疗、工业等关键领域中,使用的系统通常需要具备高可靠性和安全性。为了实现这一目标,系统通常被划分为多个功能安全领域,每个领域被称为一个“岛屿”。 Functional Safety Island 的目的是通过将系统分割成多个独立的区域,限制错误或故障的扩散,以确保系统在发生故障时仍能保持可用和安全。每个岛屿中会包含特定的功能单元,并通过安全机制和措施来监测和管理其运行状态。 每个 Functional Safety Island 都有一定的独立性和自主性,可以在一个岛屿内独立地进行故障处理和控制。这种独立性使得系统能够更好地应对单个岛屿内部发生的错误或故障,并且在某个岛屿发生故障时,其他岛屿仍然可以正常工作,从而保证整个系统的功能连续性和安全性。 Functional Safety Island 的设计和实施需要考虑到系统的功能要求、风险评估和安全标准等因素。对于每个岛屿,会进行系统设计、故障树分析、可靠性评估等工作,以确保系统的功能安全性能符合要求。 总之,Functional Safety Island 是系统安全保障的一种重要手段,通过将系统划分为多个独立的区域,限制错误或故障的扩散,提高系统的可用性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值