Dinic(邻接表存图) +当前弧优化

HDU 4280

Island Transport

*Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 16651 Accepted Submission(s): 5061
*

Problem Description

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.

Sample Input

2
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4

Sample Output

9
6

Source

2012 ACM/ICPC Asia Regional Tianjin Online

思路

裸的最大流问题,题意什么的不同说 ,关键就是用queue会超时 ,用数组模拟吧

关于当前弧优化

其实就是相当于这条边已经遍历过了,下次就不在遍历 ,我得跳过这条边 ,那么 实现:用 cur[ ] 数组,cur[ u ] = i 每次更改里面的值下次就找不到上一条边了 .

好多题解都用到当前弧优化 ,我用了跟没用耗时差不多这样讲也不对 ,因为cur[]数组赋值时候挺耗时的 ,应该是优化的时间跟耗时的时间抵消了。

每次遇到网络流我都怂,其实也不难 ,也就是代码稍微多一点 ,不过还没有线段树多 。不能吓到自己,加油!!!

在这里插入图片描述
撸代码:

#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
    int nex,to,val,from;
}edge[200010];
struct Point
{
    int x,y;
}p[100010];
int n,m,cnt,head[100010],ss,tt;
int book[100010],cur[100010],Q[400010];
void init()
{
    cnt=0;
    for(int i=0;i<=100005;i++)
        head[i]=-1;
}
void addEdge(int u,int v,int w)
{
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].val=w;
    edge[cnt].nex=head[u];
    head[u]=cnt++;

    edge[cnt].from=v;
    edge[cnt].to=u;
    edge[cnt].val=w;
    edge[cnt].nex=head[v];
    head[v]=cnt++;

}
bool bfs()
{
    memset(book,-1,sizeof(book));
    int tail=1,head1=0;
    Q[0]=ss;
    book[ss]=0;
    while(tail>head1)
    {
        int k=Q[head1++];
        for(int i=head[k];i+1;i=edge[i].nex)
        {
            int v=edge[i].to;
            if(book[v]<0&&edge[i].val>0)
            {
                book[v]=book[k]+1;
                Q[tail++]=v;
            }
        }
    }
    if(book[tt]>=0)return 1;
    return 0;
}
int dfs(int v,int sum)/*Dinic*/
{

    if(v==tt)
        return sum;
    int t,s,i,use=0;
    for(int i=cur[v];i+1;i=edge[i].nex)
    {
        cur[v]=i;/*!!!当前弧优化,每次走过这条边以后 ,再也不走了(下次跳过)*/
        int to=edge[i].to;
        if(edge[i].val>0&&book[to]==book[v]+1)
        {
            t=dfs(to,min(sum,edge[i].val));
            use+=t;
            edge[i].val-=t;
            edge[i^1].val+=t;
            sum-=t;
        }
        if(sum<=0)
            break;
    }
    if(!use)book[v]=-2;
    return use;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int minn=0x3f3f3f3f,maxx=-0x3f3f3f3f;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            if(minn>p[i].x)
            {
                minn=p[i].x;
                ss=i;
            }
            else if(maxx<p[i].x)
            {
                maxx=p[i].x;
                tt=i;
            }
        }

        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u,v,w);
        }
        int sum=0;
        while(bfs())
        {
            for(int i=1;i<=n;i++)
                cur[i]=head[i];
            sum+=dfs(ss,0x3f3f3f3f);
        }
        printf("%d\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值