Intergalactic Map SPOJ - IM
我觉得我写得已经和题解一模一样了,不知道为什么就是过不了。。懒得拍了,反正不是很难,不太想浪费时间。
1~2~3的一条路径相当于从2~1的一条路径+2~3的一条路径,点不能重复经过,于是拆点。
//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define Formylove return 0
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
const int N=;
typedef long long LL;
typedef double db;
using namespace std;
int n,m; template<typename T>void read(T &x) {
char ch=getchar(); x=; T f=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} struct edge {
int u,v,cap,fl,nx;
edge(){}
edge(int u,int v,int cap,int fl,int nx):u(u),v(v),cap(cap),fl(fl),nx(nx){}
}e[N]; int ecnt=,fir[N];
void add(int u,int v,int cap) {
e[++ecnt]=edge(u,v,cap,,fir[u]); fir[u]=ecnt;
//printf("%d->%d:%d\n",u,v,cap);
e[++ecnt]=edge(v,u,,,fir[v]); fir[v]=ecnt;
} queue<int>que;
int d[N];
void bfs(int s,int t) {
que.push(t);
For(i,,n) d[i]=n;
d[t]=;
while(!que.empty()) {
int x=que.front();
que.pop();
for(int i=fir[x];i;i=e[i].nx) {
int y=e[i].v;
if(d[y]==n&&e[i].cap==) {
d[y]=d[x]+;
que.push(y);
}
}
}
} #define inf 1e9
int p[N];
int calc(int s,int t) {
int fl=inf;
for(int i=t;i!=s;i=e[p[i]].u)
fl=min(fl,e[p[i]].cap-e[p[i]].fl);
for(int i=t;i!=s;i=e[p[i]].u)
e[p[i]].fl+=fl,e[p[i]^].fl-=fl;
return fl;
} int c[N],cur[N];
int isap(int s,int t) {
For(i,,n) c[i]=;
bfs(s,t);
For(i,,n) cur[i]=fir[i],c[d[i]]++;
int rs=;
for(int x=s;d[x]<n;) {
if(x==t) {
rs+=calc(s,t);
x=s;
}
int ok=;
for(int &i=cur[x];i;i=e[i].nx) if(e[i].cap>e[i].fl&&d[e[i].v]+==d[x]) {
ok=; p[x=e[i].v]=i; break;
}
if(!ok) {
int D=n; cur[x]=fir[x];
for(int i=fir[x];i;i=e[i].nx) if(e[i].cap>e[i].fl)
D=min(D,d[e[i].v]+);
if(!(--c[d[x]])) break;
c[d[x]=D]++;
if(x!=s) x=e[p[x]].u;
}
}
return rs;
} void init() {
ecnt=;
memset(fir,,sizeof(fir));
} int main() {
#ifdef ANS
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
int T; read(T);
while(T--) {
init();
read(n); read(m);
int s=*n+,t=s+;
For(i,,n) add(i,i+n,);
add(s,n+,);
add(,t,);
add(,t,);
For(i,,m) {
int x,y;
read(x); read(y);
if(x<||x>n||y<||y>n) continue;
add(x+n,y,);
add(y+n,x,);
}
n=t;
if(isap(s,t)==) puts("YES");
else puts("NO");
}
Formylove;
}
Intergalactic Map SPOJ - IM的更多相关文章
- SPOJ IM - Intergalactic Map - [拆点最大流]
题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...
- SPOJ 962 Intergalactic Map (网络最大流)
http://www.spoj.com/problems/IM/ 962. Intergalactic Map Problem code: IM Jedi knights, Qui-Gon Jinn ...
- SPOJ 962 Intergalactic Map
Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- SPOJ 962 Intergalactic Map (从A到B再到C的路线)
[题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...
- SPOJ 0962 Intergalactic Map
题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...
- spoj 962 IM - Intergalactic Map【最大流】
因为是无向图,所以从1到2再到3等于从2到1和3.用拆点来限制流量(i,i+n,1),然后连接(s,2+n,1),(1,t,1),(3,t,1),对于原图中的边连接(x+n,y,1)(y+n,x,1) ...
- SPOJ962 Intergalactic Map(最大流)
题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...
- SPOJ ADAFIELD Ada and Field(STL的使用:set,multiset,map的迭代器)题解
题意:n*m的方格,“0 x”表示x轴在x位置切一刀,“0 y”表示y轴在y位置切一刀,每次操作后输出当前面积最大矩形. 思路:用set分别储存x轴y轴分割的点,用multiset(可重复)储存x轴y ...
随机推荐
- vue 非父子组件之间的传值(Bus/总线/发布订阅模式/观察者模式)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue 条件渲染v-if v-show
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ssh-key添加之后依旧需要密码输入Bug的解决
场景重现 要求从10.183.93.181的root用户ssh免密登录至10.110.155.26的boss用户 1.在10.110.155.26 的boss用户下面新建目录.ssh 2.在10.11 ...
- jsp中提供的四种属性范围
参考:http://www.cnblogs.com/xdp-gacl/p/3781056.html 1.当前页:一个属性只能在一个页面中取得,跳转到其他页面无法取得 2.一次服务器请求:一个页面中设置 ...
- 冲刺$\mathfrak{CSP-S}$集训模拟赛总结
开坑.手懒并不想继续一场考试一篇文. 既没必要也没时间侧边栏的最新随笔题解反思相间也丑 而且最近越来越懒了竟然都不写题解了……开坑也是为了督促自己写题解. 并不想长篇大论.简要题解也得写啊QAQ. 目 ...
- shutil库文件的操作
一.拷贝,移动,改名 import shutil old_file=r"C:\Users\ffm11\Desktop\AI.docx" new_file=r"C:\Use ...
- 找关键字,分割字符串,输出一个vector
vector<string> split(const string& str, const string& delim) { vector<string> re ...
- java 轻量级 RestClient
package org.rx.socks.http; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; ...
- PHP面试 PHP基础知识 三(运算符)
PHP运算符 PHP的运算符的错误控制符@ PHP支持一个错误运算符:@.当将其放在一个PHP表达式之前,该表达式可能产生的任何错误信息都将会被忽略掉. PHP运算符 运算符的优先级 着重记忆运算符 ...
- 4.2.1 Vector bit-select and part-select addressing
Frm:IEEE Std 1364™-2001, IEEE Standard Verilog® Hardware Description Language Bit-selects extract a ...