C - A Plug for UNIX (又是建图坑)
题目链接:https://cn.vjudge.net/contest/68128#problem/C
没理解好题意真的麻烦,一上午就这么过去了。。。。。
具体思路:按照 源点 ->插座->转换器->插头->汇点的方式建图,源点到插座的流量为1,插头到汇点的流量为1,转换器之间如果能相连则赋值为inf.(转换器相连的条件,对于某一个转换器 他可以连下一个的头或者尾,但是这个转换器的方向不能变!!!坑点)
AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
#include<stdio.h>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 500+10
# define ll long long
int n,m,k;
int dis[maxn][maxn];
char chazuo[maxn][maxn];
char change[maxn][maxn];
char chahead[maxn][maxn];
int vis[maxn],pre[maxn];
struct node
{
char fr[maxn];
char to[maxn];
} a[maxn];
bool bfs(int st,int ed)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[st]=1;
pre[st]=st;
q.push(st);
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=1; i<=n+m+k+2; i++)
{
if(vis[i]==0&&dis[top][i]>0)
{
pre[i]=top;
vis[i]=1;
if(i==ed)return true;
q.push(i);
}
}
}
return false;
}
int EK(int st,int ed)
{
int ans=0;
while(bfs(st,ed))
{
int minn=inf;
for(int i=ed; i!=st; i=pre[i])
{
minn=min(minn,dis[pre[i]][i]);
}
for(int i=ed; i!=st; i=pre[i])
{
dis[pre[i]][i]-=minn;
dis[i][pre[i]]+=minn;
}
ans+=minn;
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
memset(dis,0,sizeof(dis));
for(int i=1; i<=n; i++)
{
scanf("%s",chazuo[i]);
}
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%*s %s",chahead[i]);
}
scanf("%d",&k);
for(int i=1; i<=k; i++)
{
scanf("%s %s",a[i].fr,a[i].to);
}
for(int i=1; i<=n; i++) //zuo -> chahead
{
for(int j=1; j<=m; j++)
{
if(strcmp(chazuo[i],chahead[j])==0)
{
dis[i][n+k+j]=1;
}
}
}
for(int i=1; i<=n; i++) //zuo -> change
{
for(int j=1; j<=k; j++)
{
if(strcmp(chazuo[i],a[j].fr)==0||strcmp(chazuo[i],a[j].to)==0)
{
dis[i][n+j]=1;
}
}
}
for(int i=1; i<=k; i++) //change-> head
{
for(int j=1; j<=m; j++)
{
if(strcmp(a[i].to,chahead[j])==0||strcmp(a[i].fr,chahead[j])==0)
{
dis[n+i][n+k+j]=1;
}
}
}
for(int i=1; i<=k; i++) // change -> change
{
for(int j=1; j<=k; j++)
{
if(strcmp(a[i].fr,a[j].to)==0)
{
dis[i+n][j+n]=inf;
}
}
}
int st=n+m+k+1;
int ed=st+1;
for(int i=1; i<=n; i++)
{
dis[st][i]=1;
}
for(int i=1; i<=m; i++)
{
dis[n+k+i][ed]=1;
}
int ans=EK(st,ed);
printf("%d\n",m-ans);
}
return 0;
}
C - A Plug for UNIX (又是建图坑)的更多相关文章
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- POJ A Plug for UNIX (最大流 建图)
Description You are in charge of setting up the press room for the inaugural meeting of the United N ...
- A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- TZOJ 1911 A Plug for UNIX(最大流)
描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
- POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
随机推荐
- three.js:Failed to execute 'texImage2D' on 'WebGLRenderingContext解决方案
three.js加载图片时,出现Failed to execute 'texImage2D' on 'WebGLRenderingContext .Tainted canvases may not b ...
- 利用VRID/VMAC实现更安全的netscaler HA故障切换
利用VRID/VMAC实现更安全的netscaler HA故障切换 virtual MAC在故障切换(failover)中的作用. 在一个HA模式中,首要节点(primary node)拥有所有 ...
- BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 989 Solved: 611[Submit][S ...
- BZOJ 2527 Meteors | 整体二分
BZOJ 2527 Meteors 题意 一个圆环上有m个位置,编号为1~m,分别属于n个国家. 有k个时刻,每个时刻都会给圆环上的一个区间中每个位置的值加上一个数. 每个国家有一个目标,问对于每个国 ...
- 【bzoj3527】 Zjoi2014—力
http://www.lydsy.com/JudgeOnline/problem.php?id=3527 (题目链接) 题意 $${F_i=\sum_{j<i} {\frac{q_iq_j}{( ...
- 方程式EQGRP_Lost_in_Translation工具之fb.py
使用方法: 环境搭建:win2003下测试: 下载python2.6并安装 下载pywin32并安装 将C:\Python26添加到环境变量PATH中 将整个windows目录复制到windows20 ...
- 洛谷P3994 Highway(树形DP+斜率优化+可持久化线段树/二分)
有点类似NOI2014购票 首先有方程$f(i)=min\{f(j)+(dep_i-dep_j)*p_i+q_i\}$ 这个显然是可以斜率优化的... $\frac {f(j)-f(k)}{dep_j ...
- 经典面试题目C语言
题一,堆和栈的区别是? 题二,Volatile与Register的区别是? 题三,ARM里的大端格式和小端格式分别是什么意思? 题一答案: (1)存储内容不同 栈:在函数调用时,栈中存放的是函数中 ...
- 设计模式之单例模式实现(C++)
#ifndef SINGLETON_H #define SINGLETON_H #include <cassert> #include <memory> #include &l ...
- 【题解】Willem, Chtholly and Seniorious Codeforces 896C ODT
Prelude ODT这个东西真是太好用了,以后写暴力骗分可以用,写在这里mark一下. 题目链接:ヽ(✿゚▽゚)ノ Solution 先把原题解贴在这里:(ノ*・ω・)ノ 简单地说,因为数据是全部随 ...