传送门

我觉得我写得已经和题解一模一样了,不知道为什么就是过不了。。懒得拍了,反正不是很难,不太想浪费时间。

1~2~3的一条路径相当于从2~1的一条路径+2~3的一条路径,点不能重复经过,于是拆点。

  1. //Achen
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<queue>
  9. #include<cmath>
  10. #include<set>
  11. #include<map>
  12. #define Formylove return 0
  13. #define For(i,a,b) for(int i=(a);i<=(b);i++)
  14. #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
  15. const int N=;
  16. typedef long long LL;
  17. typedef double db;
  18. using namespace std;
  19. int n,m;
  20.  
  21. template<typename T>void read(T &x) {
  22. char ch=getchar(); x=; T f=;
  23. while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
  24. if(ch=='-') f=-,ch=getchar();
  25. for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
  26. }
  27.  
  28. struct edge {
  29. int u,v,cap,fl,nx;
  30. edge(){}
  31. edge(int u,int v,int cap,int fl,int nx):u(u),v(v),cap(cap),fl(fl),nx(nx){}
  32. }e[N];
  33.  
  34. int ecnt=,fir[N];
  35. void add(int u,int v,int cap) {
  36. e[++ecnt]=edge(u,v,cap,,fir[u]); fir[u]=ecnt;
  37. //printf("%d->%d:%d\n",u,v,cap);
  38. e[++ecnt]=edge(v,u,,,fir[v]); fir[v]=ecnt;
  39. }
  40.  
  41. queue<int>que;
  42. int d[N];
  43. void bfs(int s,int t) {
  44. que.push(t);
  45. For(i,,n) d[i]=n;
  46. d[t]=;
  47. while(!que.empty()) {
  48. int x=que.front();
  49. que.pop();
  50. for(int i=fir[x];i;i=e[i].nx) {
  51. int y=e[i].v;
  52. if(d[y]==n&&e[i].cap==) {
  53. d[y]=d[x]+;
  54. que.push(y);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. #define inf 1e9
  61. int p[N];
  62. int calc(int s,int t) {
  63. int fl=inf;
  64. for(int i=t;i!=s;i=e[p[i]].u)
  65. fl=min(fl,e[p[i]].cap-e[p[i]].fl);
  66. for(int i=t;i!=s;i=e[p[i]].u)
  67. e[p[i]].fl+=fl,e[p[i]^].fl-=fl;
  68. return fl;
  69. }
  70.  
  71. int c[N],cur[N];
  72. int isap(int s,int t) {
  73. For(i,,n) c[i]=;
  74. bfs(s,t);
  75. For(i,,n) cur[i]=fir[i],c[d[i]]++;
  76. int rs=;
  77. for(int x=s;d[x]<n;) {
  78. if(x==t) {
  79. rs+=calc(s,t);
  80. x=s;
  81. }
  82. int ok=;
  83. for(int &i=cur[x];i;i=e[i].nx) if(e[i].cap>e[i].fl&&d[e[i].v]+==d[x]) {
  84. ok=; p[x=e[i].v]=i; break;
  85. }
  86. if(!ok) {
  87. int D=n; cur[x]=fir[x];
  88. for(int i=fir[x];i;i=e[i].nx) if(e[i].cap>e[i].fl)
  89. D=min(D,d[e[i].v]+);
  90. if(!(--c[d[x]])) break;
  91. c[d[x]=D]++;
  92. if(x!=s) x=e[p[x]].u;
  93. }
  94. }
  95. return rs;
  96. }
  97.  
  98. void init() {
  99. ecnt=;
  100. memset(fir,,sizeof(fir));
  101. }
  102.  
  103. int main() {
  104. #ifdef ANS
  105. freopen(".in","r",stdin);
  106. freopen(".out","w",stdout);
  107. #endif
  108. int T; read(T);
  109. while(T--) {
  110. init();
  111. read(n); read(m);
  112. int s=*n+,t=s+;
  113. For(i,,n) add(i,i+n,);
  114. add(s,n+,);
  115. add(,t,);
  116. add(,t,);
  117. For(i,,m) {
  118. int x,y;
  119. read(x); read(y);
  120. if(x<||x>n||y<||y>n) continue;
  121. add(x+n,y,);
  122. add(y+n,x,);
  123. }
  124. n=t;
  125. if(isap(s,t)==) puts("YES");
  126. else puts("NO");
  127. }
  128. Formylove;
  129. }

Intergalactic Map SPOJ - IM的更多相关文章

  1. SPOJ IM - Intergalactic Map - [拆点最大流]

    题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...

  2. SPOJ 962 Intergalactic Map (网络最大流)

    http://www.spoj.com/problems/IM/ 962. Intergalactic Map Problem code: IM Jedi knights, Qui-Gon Jinn ...

  3. SPOJ 962 Intergalactic Map

    Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...

  4. [SPOJ962]Intergalactic Map 拆点+最大流

    Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...

  5. SPOJ 962 Intergalactic Map (从A到B再到C的路线)

    [题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...

  6. SPOJ 0962 Intergalactic Map

    题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...

  7. 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) ...

  8. SPOJ962 Intergalactic Map(最大流)

    题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...

  9. 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 ...

随机推荐

  1. mysql类型转换函数convert与cast的用法,及SQL server的区别

    首先,convert函数 字符集转换 :   CONVERT(xxx  USING   gb2312) 类型转换和SQL Server一样,不过类型参数上有不同: CAST(xxx  AS   类型) ...

  2. Windows API 查看器

    { http://www.dependencywalker.com/ } { Download the latest version here:  Download Version 2.2.6000 ...

  3. git——修改已经提交并push后的commit注释

    把代码push到远程后,发现commit的注释居然多了几个错别字,不行,必须改了! 搜索了一些答案之后自己做了一个总结如下: ①修改倒数第次的commit 指令:$ git rebase -i HEA ...

  4. go导入包

    go导入包 go有很多内置的函数,例如println,不需要引用即可使用.但是如果不借助go的标准库或者第三方库,我们能做的事情有限.在go中,使用关键字import在代码中导入一个包并使用. 修改我 ...

  5. 使用Objective-C的+(void)initialize初始化static变量

    在<Objective C类方法load和initialize的区别>一文中,我介绍了Objective-C对待+(void)initialize和+(void)load两个方法在编译和执 ...

  6. STM32嵌入式开发学习笔记(一)

    本文中,笔者将介绍使用嵌入式开发工具Keil uVision5,使用C语言,对微处理器STM32F103C8进行嵌入式开发. 开发使用C语言,首先需要新建一个C语言文件,将其设为主函数的入口,因此,将 ...

  7. cgroup & oom-killer 简介

    cgroup内存限制 memory.failcnt memory.limit_in_bytes memory.usage_in_bytes memory.max_usage_in_bytes memo ...

  8. CSS:CSS 边框

    ylbtech-CSS:CSS 边框 1.返回顶部 1. CSS 边框 CSS 边框属性 边框样式 边框样式属性指定要显示什么样的边界.  border-style属性用来定义边框的样式 border ...

  9. c# 使用 java的 rsa 进行签名

    /// <summary> /// 类名:RSAFromPkcs8 /// 功能:RSA加密.解密.签名.验签 /// 详细:该类对Java生成的密钥进行解密和签名以及验签专用类,不需要修 ...

  10. delphi基础篇之数据类型

      Object Pascal 数据类型    数据类型与定义变量 Object Pascal 语言的最大特点是对数据类型的要求非常严谨.传递给过程或函数的参数值必须与形参的类型一致.在Object ...