UVA 753 A Plug for UNIX

本文介绍了一种使用ISAP算法解决最大流问题的方法,通过构建复杂的图模型来模拟插板、设备及转换器间的匹配过程。文章详细展示了如何将设备、转换器等元素转化为图中的节点,并建立合适的边以反映它们之间的匹配关系。

n个插板,m台设备,k种转换器,一开始WA了一次,原来是忘了每种转换器的数目时无限多的,建立边时需要注意 和UVA 11045 My T-shirt suits me  有点类似,稍微复杂一点,并且需要查询 匹配再添加相应的边。

大体思路:

将每台设备拆分成两点,建立容量为1的边,并且建立从源点0到该设备拆分点的一条边,容量为1;每个转换器同样拆分建立容量无限的边,然后插板拆分,建立容量为1的边,再把插板拆分后的点连接到汇点1,建立容量为1的边。

接下来便是查找相应匹配接口, 如对设备,需要查找能够和它的插口匹配的转换器和插板,对转换器则需要查找匹配其他转换器和所有的插板,对所有查找到的匹配建立相应的边,容量为1。

然后利用ISAP求解0-->>>1的最大流即可。

似乎对这样的小数据 ,spfa算法似乎能够快很多。

代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <cctype>
#include <queue>
#include <algorithm>
#include <cmath>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define print(a) printf("%d\n", (a));
#define bug  puts("(****");
#define judge  freopen("in.txt","r",stdin);
using namespace std;
#define N 10000
#define M 400000
struct EDGE
{
    int i, c;
    EDGE *next, *ani;
} *Edge[N], E[M];
int Dfn[N], Now[N], cnt;
typedef struct
{
    char a[100];
    char b[100];
    int type;
} sft;
sft node[N];
int n, m, k, src, sink;
void add(int i, int j, int c, EDGE &e1, EDGE &e2)
{
    e1.i = j, e1.c = c, e1.next = Edge[i], e1.ani = &e2, Edge[i] = &e1;
    e2.i = i, e2.c = 0, e2.next = Edge[j], e2.ani = &e1, Edge[j] = &e2;
}
void init(void)
{
    memset(Edge, 0, sizeof(Edge));
    src = cnt = 0;
    sink = 1;
}
bool f(int i, int j)
{
  return !strcmp(node[i].b, node[j].a);
}
int ISAP(int s, int end, int flow)
{
    if(s == end)
        return flow;
    int i, tab = n-1, vary, now = 0;
    for(EDGE *p =Edge[s]; p && flow - now; p = p->next)
        if(p->c)
        {
            if(Dfn[s] == Dfn[i = p->i] + 1)
                vary = ISAP(i, end, min(flow - now, p->c)),
                p->c -= vary, now += vary, p->ani->c += vary;
            if(p->c)
                tab = min(tab, Dfn[i]);
            if(Dfn[src] == n)
                return now;
        }
    if(now == 0)
    {
        if(--Now[Dfn[s]] == 0)
            Dfn[src] = n;
        Now[Dfn[s] = tab +1]++;
    }
    return now;
}
int Max_flow(int s, int end)
{
    memset(Dfn, 0, sizeof(Dfn));
    memset(Now, 0, sizeof(Now));
    Now[0] = n;
    int ret = 0;
    for(; Dfn[s] < n;)
    {
        ret += ISAP(s, end, inf);
    }
    return ret;
}
int main(void)
{
    int T;
    for(int t = scanf("%d", &T); t <= T; t++)
    {
        init();
        int ct = 1;
        for(int i = scanf("%d", &n); i <= n; i++, ct++)
        {
            scanf("%s", node[ct].a);
            node[ct].type = 1;
            strcpy(node[ct].b, node[ct].a);
            add(2*ct^1, sink, 1, E[cnt],E[cnt + 1]);
            cnt += 2;
            add(2*ct, 2*ct^1, 1, E[cnt], E[cnt + 1]);
            cnt += 2;
        }
        for(int i = scanf("%d", &m); i <= m; i++, ct++)
        {
            scanf("%*s%s", node[ct].a);
            node[ct].type = 2;
            strcpy(node[ct].b, node[ct].a);
            add(0, 2*ct, 1, E[cnt], E[cnt + 1]);
            cnt += 2;
            add(2*ct, 2*ct^1, 1, E[cnt], E[cnt + 1]);
            cnt += 2;
        }
        for(int i = scanf("%d", &k); i <= k; i++, ct++)
        {
            scanf("%s%s", node[ct].a, node[ct].b);
            node[ct].type = 3;
            add(2*ct, 2*ct^1, inf, E[cnt], E[cnt + 1]);
            cnt += 2;
        }
        for(int i = n + 1; i <= n+m+k; i++)
            for(int j = 1; j <= n+m+k; j++)
                if(i - j && node[j].type != 2)
                {
                    if(f(i, j))
                        add(2*i^1, 2*j, 1, E[cnt], E[cnt + 1]),
                            cnt += 2;
                }
        n = 2*n+2*m+2*k+2;
        int ans = Max_flow(src, sink);
        ans = m - ans;
        if(t - 1)
           puts("");
        printf("%d\n",ans);
    }
    return 0;
}



A Different Task(UVA 10795)是汉诺塔问题的一个变形,其特是圆盘初始可以在任意柱子上。 ### 解题思路 该问题可以通过将初始情况和终止情况通过一个中间状态联系起来求解。先找到最大的且初始位置和最终位置不同的圆盘,记为第 `k` 个圆盘。然后将比第 `k` 个圆盘小的所有圆盘移动到一个中间柱子上,接着移动第 `k` 个圆盘到目标柱子,最后再将中间柱子上的圆盘移动到目标柱子。 ### 代码实现 以下是两种不同的代码实现: #### 代码实现一 ```cpp #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define MAXN 70 int a[MAXN],b[MAXN]; ll solve(int from,int k,int *p) { while(k >= 0 && from == p[k])k--; if(k < 0)return 0; return solve(6 - p[k] - from,k - 1,p) + ((ll)1 << k); } int main() { int kcas = 1,n; while(cin >> n && n) { for(int i = 0;i < n;i++)cin >> a[i]; for(int i = 0;i < n;i++)cin >> b[i]; int k = n - 1; while(k >= 0 && a[k] == b[k])k--; ll ans = 0; if(k >= 0) { int other = 6 - a[k] - b[k]; ans = solve(other,k - 1,a) + solve(other,k - 1,b) + 1; } printf("Case %d: %lld\n",kcas++,ans); } return 0; } ``` #### 代码实现二 ```cpp #include<cstdio> #include<cstring> using namespace std; #define maxn 100 int start[maxn], end[maxn]; long long move(int *temp, int num, int Final) { if(num == 0) return 0; if(temp[num] == Final) return move(temp, num - 1, Final); return move(temp,num-1,6-temp[num]-Final) + (1LL << ( num - 1)); } int main() { int N, mark = 1; while(scanf("%d",&N) == 1 && N) { for(int i = 1; i <= N; i++) scanf("%d",&start[i]); for(int i = 1; i <= N; i++) scanf("%d",&end[i]); int MAX = N; while(MAX >= 1 && start[MAX] == end[MAX]) MAX--; long long ans = 0; if(MAX >= 1) { int Final = 6 - start[MAX] - end[MAX]; ans = move(start,MAX-1,Final) + move(end,MAX-1,Final) + 1; } printf("Case %d: %lld\n",mark++, ans); } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值