UVALIVE 3645 Objective: Berlin

最大流 。以航班为节点进行最大流。 容量限制进行拆点。 如果时间地点满足可以建一条边。 具体看代码。变量名被修改过了。一开始的变量名可能比较容易看懂

但CE了。可能与库里的变量重复了。

AC代码

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 15050
const int INF = 0x3f3f3f3f ;
struct node
{
    int u,v,next;
    int cap,flow;
}edge[1000005];
int cnt,N,M;
int head[MAXN];
int ans;
string stpos,edpos,sti,eti;
map<string,int>place;
struct flight
{
    int s,t;
    int statime,ndtime;
    int num;
}src[MAXN];
int cas,lasttime,a[MAXN];
queue<int>q;
void addedge(int u, int v, int cap)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = cap;
    edge[cnt].flow = 0;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = 0;
    edge[cnt].flow = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
void read()
{
    place.clear();
    cas = 1;
    cin >> stpos >> edpos;
    place[stpos] = cas++; place[edpos] = cas++;
    cin >> eti;
    lasttime = ((eti[0] - ‘0‘) * 10 + eti[1] - ‘0‘) * 60 + (eti[2] - ‘0‘) * 10 + eti[3] - ‘0‘;
    cin >> M;
    for (int i = 1; i <= M; i++)
    {
        cin >> stpos >> edpos;
        if (!place[stpos]) place[stpos] = cas++;
        if (!place[edpos]) place[edpos] = cas++;
        cin >> src[i].num;
        cin >> sti >> eti;
        src[i].statime = ((sti[0] - ‘0‘) * 10 + sti[1] - ‘0‘) * 60 + (sti[2] - ‘0‘) * 10 + sti[3] - ‘0‘;
        src[i].ndtime = ((eti[0] - ‘0‘) * 10 + eti[1] - ‘0‘) * 60 + (eti[2] -‘0‘) * 10 + eti[3] - ‘0‘;
        src[i].s = place[stpos];
        src[i].t = place[edpos];
    }
}
void build()
{
    memset(head,-1,sizeof(head));
    cnt = 0;
    for (int i = 1; i <= M; i++)
    {
        if (src[i].s == 1) addedge(0,i,INF);
        if (src[i].t == 2 && src[i].ndtime <= lasttime) addedge(i + M,M * 2 + 1,INF);
        addedge(i, i + M, src[i].num);
        for (int j = 1; j <= M; j++)
        {
            if (i == j)continue;
            if (src[i].t == src[j].s && src[i].ndtime + 30 <= src[j].statime) addedge(i + M, j,INF);
        }
    }
}
int Edmonds_karp(int source,int target)
{
    while (!q.empty()) q.pop();
    int p[MAXN];
    int F = 0;
    while (true)
    {
       memset(p,-1,sizeof(p));
       q.push(source);
       memset(a,0,sizeof(a));
       a[source] = INF;
       while (!q.empty())
       {
           int u = q.front(); q.pop();
           for (int i = head[u]; i != -1; i = edge[i].next)
           {
               int v = edge[i].v;
               if (!a[v] && edge[i].cap > edge[i].flow)
               {
                   a[v] = min(a[u],edge[i].cap - edge[i].flow);
                   p[v] = i;
                   q.push(v);
               }
           }
       }
       if (a[target] == 0) break;
       for (int i = p[target]; i != -1; i = p[edge[i].u])
       {
           edge[i].flow += a[target];
           edge[i ^ 1].flow -= a[target];
       }
       F += a[target];
    }
    return F;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    while (scanf("%d",&N) != EOF)
    {
        read();
        build();
        printf("%d\n",Edmonds_karp(0, 2 * M + 1));
    }
    return 0;
}

CE代码也放在这里容易看懂一些

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 15050
const int INF = 0x3f3f3f3f ;
struct node
{
    int u,v,next;
    int cap,flow;
}edge[1000005];
int cnt,N,M;
int head[MAXN];
int ans;
string stpos,edpos,stime,etime;
map<string,int>place;
struct flight
{
    int s,t;
    int sttime,edtime;
    int num;
}src[MAXN];
int cas,lasttime,a[MAXN];
queue<int>q;
void addedge(int u, int v, int cap)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = cap;
    edge[cnt].flow = 0;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = 0;
    edge[cnt].flow = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
void read()
{
    place.clear();
    cas = 1;
    cin >> stpos >> edpos;
    place[stpos] = cas++; place[edpos] = cas++;
    cin >> etime;
    lasttime = ((etime[0] - ‘0‘) * 10 + etime[1] - ‘0‘) * 60 + (etime[2] - ‘0‘) * 10 + etime[3] - ‘0‘;
    cin >> M;
    for (int i = 1; i <= M; i++)
    {
        cin >> stpos >> edpos;
        if (!place[stpos]) place[stpos] = cas++;
        if (!place[edpos]) place[edpos] = cas++;
        cin >> src[i].num;
        cin >> stime >> etime;
        src[i].sttime = ((stime[0] - ‘0‘) * 10 + stime[1] - ‘0‘) * 60 + (stime[2] - ‘0‘) * 10 + stime[3] - ‘0‘;
        src[i].edtime = ((etime[0] - ‘0‘) * 10 + etime[1] - ‘0‘) * 60 + (etime[2] - ‘0‘) * 10 + etime[3] - ‘0‘;
        src[i].s = place[stpos];
        src[i].t = place[edpos];
    }
}
void build()
{
    memset(head,-1,sizeof(head));
    cnt = 0;
    for (int i = 1; i <= M; i++)
    {
        if (src[i].s == 1) addedge(0,i,INF);
        if (src[i].t == 2 && src[i].edtime <= lasttime) addedge(i + M,M * 2 + 1,INF);
        addedge(i, i + M, src[i].num);
        for (int j = 1; j <= M; j++)
        {
            if (i == j)continue;
            if (src[i].t == src[j].s && src[i].edtime + 30 <= src[j].edtime) addedge(i + M, j,INF);
        }
    }
}
int Edmonds_karp(int source,int target)
{
    while (!q.empty()) q.pop();
    int p[MAXN];
    int F = 0;
    while (true)
    {
       memset(p,-1,sizeof(p));
       q.push(source);
       memset(a,0,sizeof(a));
       a[source] = INF;
       while (!q.empty())
       {
           int u = q.front(); q.pop();
           for (int i = head[u]; i != -1; i = edge[i].next)
           {
               int v = edge[i].v;
               if (!a[v] && edge[i].cap > edge[i].flow)
               {
                   a[v] = min(a[u],edge[i].cap - edge[i].flow);
                   p[v] = i;
                   q.push(v);
               }
           }
       }
       if (a[target] == 0) break;
       for (int i = p[target]; i != -1; i = p[edge[i].u])
       {
           edge[i].flow += a[target];
           edge[i ^ 1].flow -= a[target];
       }
       F += a[target];
    }
    return F;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    while (scanf("%d",&N) != EOF)
    {
        read();
        build();
        printf("%d\n",Edmonds_karp(0, 2 * M + 1));
    }
    return 0;
}

时间: 2024-11-10 22:58:18

UVALIVE 3645 Objective: Berlin的相关文章

uva 1161 Objective: Berlin (最大流)

uva 1161 Objective: Berlin 题目大意:你要从A地到B地去,并且最晚要在lt之前到达.现在给你m个航班信息,信息包括:起始地点,降落地点,载客上限,起飞时间,降落时间.中途转机要花费半小时的时间.问在lt之前,可以从A地到达B地的最多的游客数量. 解题思路:以航班为节点进行建图.设置超级源点,连向所有起点为A地的航班,容量为INF:设置超级汇点,使所有降落地点为B点且降落时间在lt之前的航班连向超级汇点.每个航班都要拆成两个节点,中间的边容量为该航班的载客上限.如果i航班

UVA 1161 - Objective: Berlin(网络流)

UVA 1161 - Objective: Berlin 题目链接 题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市 思路:以航班为结点建图,航班有容量限制所以进行拆点,然后两个航班如果终点和起点对上,并且时间满足就可以建边,然后源点连向起点为起始的航班,终点为终点的航班连向汇点(要在时间不超过时限的情况下),建好图跑一下最大流就可以了 代码: #include <cstdio> #include <cstring&g

UVa1161 Objective: Berlin(最大流)

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3602 Description The administration of a well-known football team has made a study about the lack of support ininternational away games. This st

UVa 1161 Objective: Berlin (最大流)

题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市. 析:差不多是裸板网络流的最大流问题,把每个航班都拆成两个点,这两个点之间连接一条流量为这个航班的容量,然后再暴力去查看能不能连接,如果能, 那么就连接一条容量无限的边,然后在源点和汇点加一个无限的容量边. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #

UVALive 3211 Now or later

Now or later Time Limit: 9000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 321164-bit integer IO format: %lld      Java class name: Main As you must have experienced, instead of landing immediately, an aircraft someti

【贪心】UVALive 6530——Football

Your favorite football team is playing a charity tournament, which is part of a worldwide fundraising e ort to help children with disabilities. As in a normal tournament, three points are awarded to the team winning a match, with no points to the los

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从