SPOJ 962 Intergalactic Map (从A到B再到C的路线)
【题意】在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点。问是否存在这样的路线。(3 <= N <= 30011, 1 <= M <= 50011)
【思路】很巧的一道题,一般我们都是把源点连接起点,但那样的话就不好控制从A先到B再到C了,所以我们换个思路,以B为源点,A、C为汇点,看最大流是否为2即可~不经过同一个点就直接拆点连一条(i, i', 1)即可,无向图……就连两条反向边吧~~本来想改一下反向流就好的,可是想想那样也把源点汇点连出来的边也变成双向了……没试行不行……
#include
#include
#include
#include
#include
#include
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 60055;
const int MAXE = 200055;
const int oo = 0x3fffffff;
struct node{
int u, v, flow;
int opp;
int next;
};
struct Dinic{
node arc[2*MAXE];
int vn, en, head[MAXV]; //vn点个数(包括源点汇点),en边个数
int cur[MAXV]; //当前弧
int q[MAXV]; //bfs建层次图时的队列
int path[2*MAXE], top; //存dfs当前最短路径的栈
int dep[MAXV]; //各节点层次
void init(int n){
vn = n;
en = 0;
mem(head, -1);
}
void insert_flow(int u, int v, int flow){
arc[en].u = u;
arc[en].v = v;
arc[en].flow = flow;
arc[en].opp = en + 1;
arc[en].next = head[u];
head[u] = en ++; arc[en].u = v;
arc[en].v = u;
arc[en].flow = 0; //反向弧
arc[en].opp = en - 1;
arc[en].next = head[v];
head[v] = en ++;
}
bool bfs(int s, int t){
mem(dep, -1);
int lq = 0, rq = 1;
dep[s] = 0;
q[lq] = s;
while(lq 0){
dep[v] = dep[u] + 1;
q[rq ++] = v;
}
}
}
return false;
}
int solve(int s, int t){
int maxflow = 0;
while(bfs(s, t)){
int i, j;
for (i = 1; i arc[path[k]].flow){
minflow = arc[path[k]].flow;
mink = k;
}
for (int k = 0; k n || v > n) continue;
dinic.insert_flow(2*u, 2*v-1, 1);
dinic.insert_flow(2*v, 2*u-1, 1);
}
dinic.insert_flow(2*n+1, 2*2, 2);
dinic.insert_flow(2*1, 2*n+2, 1);
dinic.insert_flow(2*3, 2*n+2, 1);
if (dinic.solve(2*n+1, 2*n+2) == 2){
puts("YES");
}
else{
puts("NO");
}
}
return 0;
}
SPOJ 962 Intergalactic Map (从A到B再到C的路线)的更多相关文章
- 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 ...
- SPOJ IM - Intergalactic Map - [拆点最大流]
题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...
- SPOJ 0962 Intergalactic Map
题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- 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) ...
- Intergalactic Map SPOJ - IM
传送门 我觉得我写得已经和题解一模一样了,不知道为什么就是过不了..懒得拍了,反正不是很难,不太想浪费时间. 1~2~3的一条路径相当于从2~1的一条路径+2~3的一条路径,点不能重复经过,于是拆点. ...
- SPOJ - ADAFIELD ,Set+map,STL不会超时!
ADAFIELD - Ada and Field 这个题,如果用一个字来形容的话:-----------------------------------------------嗯! 题意:n*m的空白 ...
- SPOJ962 Intergalactic Map(最大流)
题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...
随机推荐
- jooml二次开发---添加文章组件
在写一个joomla组件的时候需要手动添加excel表格,并把表格当做文章的内容添加到前台文章中, 开始不知道怎么下手,索性先把一个基本的组件写出来,在joomla网站上测试是可以访问这个组件的,在p ...
- PHP核心代码库中的APC缓存说明123
1.APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓存 ...
- Hello,iOS
xcode 6.1 File-New-Project.. iOs-Application-Simple View Application Main.storyboard ==> 拖一个TextV ...
- Arcgis 10.1中空间连接功能
空间链接的作用:将面上的所有点的值加起来取平均值.赋值给面属性.(我们可以定义右击——定义合并规则 连接要素的字段映射参数中指定的合并规则仅适用于连接要素中的属性,且仅适用于多个要素与目标要素匹配 ( ...
- Technical diagrams for SharePoint 2013
sharepoint2013技术图表 http://technet.microsoft.com/zh-cn/library/cc263199.aspx SharePoint 2013 的可下载内容 h ...
- ExtJS4.2学习(九)属性表格控件PropertyGrid(转)
鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-15/178.html ------------- ...
- 【斜率DP】BZOJ 1911:特别行动队
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3006 Solved: 1360[Submit][Statu ...
- [转载]jquery cookie的用法
原文地址:http://www.cnblogs.com/qiantuwuliang/archive/2009/07/19/1526663.html jQuery cookie是个很好的cookie插件 ...
- PHP MSSQL数据操作PDO API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- PHP程序员函数注释规格
<?php/*** @name 名字* @abstract 申明变量/类/方法* @access 指明这个变量.类.函数/方法的存取权限* @author 函数作者的名字和邮箱地址* @cate ...