uva 11380 - Down Went The Titanic (最大流)

该问题要求在考虑浮动冰块、水域、大冰山和安全木头等条件下,找出使最多人数幸存的策略。输入包含多个测试用例,每个用例描述了一个区域的X、Y尺寸和大木头的最大承载人数P,输出为每个测试用例的最大幸存者数。解题关键在于正确理解人们在安全地点可以停留直至救援船到达。

Problem D

Down Went The Titanic

Time Limit: 8 Second

 

After the collision of the great Titanic with the iceberg, it went down. Now there are peoples floating in the cold water struggling with death. Some helping ship will arrive to save them. But they have to survive until the ships arrive. Now consider a water area with people, floating ices, large woods etc. Consider the following symbols:

 

*          People staying on floating ice. People want to move from here as the floating ice cannot carry them for long time. Once a people move from here, the floating ice will get drowned. People can move to any of the four directions (north, east, west and south).

~          Water. People cannot go or move through them as water is extremely cold and not good enough for swimming.

.           Floating ice. People can move to a floating ice. But floating ices are so light that they cannot float for long time, so people should move from here as soon as possible and once a people move from here, the floating ice will get drowned.

@        Large iceberg. People can move here but cannot stay here as they are extremely cold. These icebergs will remain floating all the time. Note that, no two people can stay on floating ice or large iceberg at the same time.

#          Large wood. This place is safe. People can move and stay here until the helping ships arrive. A large wood will get drowned if more than P people stay on it at the same time.

 

Given the description of the area you have to find an optimal strategy that ensures the maximum number of living people.

 

Input:

The input contains a number of test cases. Each test case starts with a line containing three integers X, Y and P, where X, Y is the dimensions of the area (1 ≤ X, Y ≤ 30) and P (P ≤ 10) is the highest capacity of the large woods.  Next X lines each contains Y characters. These lines contain no blank spaces or any characters other than asterisk (*), tilde (~), dot (.), at (@) and hash (#). Not more than 50% of the total area has a people. Input will terminate with end of file (EOF). There is a blank line between two consecutive test cases.

 

Output:

For each test case print one line of output, an integer denoting the maximum number of survivors possible.

 

SAMPLE INPUT

OUTPUT FOR SAMPLE INPUT

3 4 2

*~~#

...@

.~.*

 

3 5 1

~~*~~

#.@.#

~~*~~

 

1 4 2

**#~

2

2

1

 

 

 

Problemsetter: Ishtiak Zaman

Special Thanks To: Md. Mahbubul Hasan

根据题目意思建图就可以了。开始错在误认为走到#上后就不会再移动了,实际上题目根本没有这样的条件。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2000 + 5;
const int maxm = 100;
const int INF = 1000000000;

struct Edge {
  int from, to, cap, flow;
};

struct Dinic {
  int n, m, s, t;
  vector<Edge> edges;    // 边数的两倍
  vector<int> G[maxn];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
  bool vis[maxn];        // BFS使用
  int d[maxn];           // 从起点到i的距离
  int cur[maxn];         // 当前弧指针

  void ClearAll(int n) {
    for(int i = 0; i < n; i++) G[i].clear();
    edges.clear();
  }

  void ClearFlow() {
    for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
  }

  void AddEdge(int from, int to, int cap) {
    //cout << from << ' ' << to << ' ' << cap << endl;
    edges.push_back((Edge){from, to, cap, 0});
    edges.push_back((Edge){to, from, 0, 0});
    m = edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
  }

  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    while(!Q.empty()) {
      int x = Q.front(); Q.pop();
      for(int i = 0; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i]];
        if(!vis[e.to] && e.cap > e.flow) {
          vis[e.to] = 1;
          d[e.to] = d[x] + 1;
          Q.push(e.to);
        }
      }
    }
    return vis[t];
  }

  int DFS(int x, int a) {
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i < G[x].size(); i++) {
      Edge& e = edges[G[x][i]];
      if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0) {
        e.flow += f;
        edges[G[x][i]^1].flow -= f;
        flow += f;
        a -= f;
        if(a == 0) break;
      }
    }
    return flow;
  }

  int Maxflow(int s, int t) {
    this->s = s; this->t = t;
    int flow = 0;
    while(BFS()) {
      memset(cur, 0, sizeof(cur));
      flow += DFS(s, INF);
    }
    return flow;
  }
};

Dinic g;

char M[maxm][maxm];
int x, y;

int id(int i, int j){
    return (i-1)*y+j;
}

const int cx[] = {-1,0,1,0};
const int cy[] = {0,1,0,-1};

void expand(int nx, int ny, int pos){
    for(int i = 0;i < 4;i++){
        int tx = nx+cx[i];
        int ty = ny+cy[i];
        if(M[tx][ty] == '.' || M[tx][ty] == '@' || M[tx][ty] == '#'){
            g.AddEdge(pos, id(tx, ty), INF);
        }
    }
}

int main(){
    int p;
    while(scanf("%d%d%d", &x, &y, &p)!= EOF){
        g.ClearAll(x*y*2+2);
        int source = 0, sink = x*y*2+1;
        memset(M, -1, sizeof M);
        for(int i = 1;i <= x;i++){
            scanf("%s", M[i]+1);
        }
        for(int i = 1;i <= x;i++){
            for(int j = 1;j <= y;j++){
                if(M[i][j] == '*'){
                    expand(i, j, id(i, j));
                    g.AddEdge(source, id(i, j), 1);
                }
                else if(M[i][j] == '.'){
                    g.AddEdge(id(i, j), id(i, j)+x*y, 1);
                    expand(i, j, id(i, j)+x*y);
                }
                else if(M[i][j] == '@'){
                    expand(i, j, id(i, j));
                }
                else if(M[i][j] == '#'){
                    g.AddEdge(id(i, j), sink, p);
                    expand(i, j, id(i, j));
                }
            }
        }
        printf("%d\n", g.Maxflow(source, sink));
    }
    return 0;
}


在数字化环境中,线上票务获取已成为参与各类活动的主要途径。随着公众对热门演出需求的增长,票源往往在开放销售后迅速告罄,导致普通消费者难以顺利购得所需票券。为应对这一挑战,部分技术开发者借助编程手段构建了自动化购票辅助程序,旨在提升用户成功获取门票的概率。本文将以一个针对特定票务平台设计的自动化工具为例,系统阐述其设计理念、技术组成及具体实施流程。 秀动网作为国内知名的演出及体育赛事票务销售平台,因活动热度较高,常出现访问拥堵、瞬时抢购压力大等现象,使得常规购票过程面临困难。因此,开发一款能够协助用户更有效完成票务申购的辅助工具具有实际意义。 该工具主要具备以下几项关键功能:持续监控目标平台的票务信息更新;在票务释放时自动执行选座、添加至购物车及提交订单等系列操作;集成一定的异常处理机制,以应对网络延迟或服务器响应异常等情况。 在技术实现层面,选用Python作为开发语言,主要基于其语法简洁、标准库与第三方资源丰富,适合快速构建功能原型。同时,Python在网络通信与浏览器自动化方面拥有如requests、selenium等成熟支持库,为程序实现网页交互与数据抓取提供了便利。 开发过程主要包括以下环节:首先解析目标网站的页面结构,明确可通过程序操控的网页元素路径;随后编写监控模块,实时检测新票务信息的上线并及时触发后续操作;接着模拟用户操作流程,包括自动填写个人信息、选择座位偏好、完成购物车添加等步骤,并通过行为模拟降低被平台反爬虫机制识别的可能;最终实现订单自动提交,并在成功购票后向用户发送通知。 此外,该工具提供了可配置的操作界面,允许用户根据个人需求设定抢票时间、目标活动类型及座位选择等参数,从而在提升使用体验的同时,减少对票务平台服务器资源的非必要占用。 需指出的是,尽管此类工具能提高购票效率,但其使用可能涉及违反平台服务协议或相关法规的风险。各票务销售方通常对自动化抢票行为设有明确约束,因此开发与使用者均应遵守相应规定,确保技术应用的合法性。 综上所述,该基于Python的票务辅助工具是针对特定场景设计的自动化解决方案,通过技术手段改善用户购票体验,但同时也强调必须在法律与平台规则框架内合理使用此类技术。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
### 解决泰坦尼克号生存预测中因变量未定义导致的NameError问题 在处理泰坦尼克号生存预测数据时,如果遇到`NameError: name 'titanic' is not defined`错误,通常是因为变量`titanic`未被正确定义或加载。以下是对该问题的专业解决方案。 #### 数据加载与变量定义 确保数据已正确加载到变量`titanic`中。使用`pandas`库加载CSV文件时,代码应如下所示: ```python import pandas as pd titanic = pd.read_csv('titanic.csv') # 确保文件路径正确[^1] ``` 如果文件路径不正确或文件名拼写错误,将导致`titanic`未被定义。 #### 检查变量是否已定义 在执行任何操作之前,可以检查变量`titanic`是否已定义。例如,通过打印其前几行来验证: ```python print(titanic.head()) # 如果变量未定义,此行将抛出NameError ``` #### 使用`dropna`方法时的注意事项 在调用`dropna`方法之前,必须确保`titanic`变量已被成功加载。以下是正确的`dropna`调用示例: ```python titanic.dropna(inplace=True) # 删除包含缺失值的所有行[^2] ``` 如果在此处出现`NameError`,则表明`titanic`变量未被正确定义。 #### 常见原因及解决方法 1. **文件路径错误**:确保`'titanic.csv'`文件位于当前工作目录下,或者提供完整的文件路径。 ```python titanic = pd.read_csv('/path/to/titanic.csv') # 替换为实际路径 ``` 2. **变量名拼写错误**:检查变量名是否拼写正确。例如,避免将`titanic`误写为`Titanic`或`tiTanic`。 3. **代码执行顺序问题**:确保在调用`titanic.dropna()`之前已成功执行`titanic = pd.read_csv('titanic.csv')`。 #### 示例代码 以下是一个完整的示例代码,展示如何加载数据并删除缺失值: ```python import pandas as pd # 加载数据 titanic = pd.read_csv('titanic.csv') # 确保文件路径正确[^1] # 检查数据是否加载成功 print(titanic.head()) # 删除包含缺失值的行 titanic.dropna(inplace=True) # 删除缺失值[^2] # 输出处理后的数据 print(titanic.info()) ``` #### 注意事项 - 如果`titanic.csv`文件不存在或路径错误,将导致`titanic`变量未定义。 - 在使用`dropna`方法时,确保`titanic`变量已正确定义并加载数据。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值