poj1637Sightseeing tour
网络流解混合图欧拉回路,以前xy讲过,但是我一直没写。
把无向边随意定向,每个点权值为出度减入度,权值为奇数无解,权值大于0的从s向其连权值/2的边,小于0的向t连-权值/2的边,原图中无向图按定向连u->v权值为1的边,跑网络流判断是否满流即可,原图中的满流边即为要取反的边。
这两天先悠闲地整理一下前几天学的内容,过两天再开始全力准备noip吧大概。。
//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;
} int dd[N];
vector<int>vc;
void init() {
ecnt=;
memset(fir,,sizeof(fir));
memset(dd,,sizeof(dd));
} int main() {
#ifdef ANS
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
int T; read(T);
while(T--) {
init();
read(n); read(m);
For(i,,m) {
int u,v,w;
read(u); read(v); read(w);
dd[u]++; dd[v]--;
if(!w) add(u,v,);
}
int s=n+,t=n+,fl=; n+=;
For(i,,n-) {
if(dd[i]&) { fl=; break; }
if(dd[i]>) add(s,i,dd[i]/);
if(dd[i]<) add(i,t,(-dd[i])/);
if(dd[i]!=) vc.push_back(ecnt-);
}
isap(s,t);
int up=vc.size();
For(i,,up-) if(e[vc[i]].fl!=e[vc[i]].cap) {
fl=; break;
} vc.clear();
if(fl) puts("impossible");
else puts("possible");
}
Formylove;
}
poj1637Sightseeing tour的更多相关文章
- poj1637--Sightseeing tour(最大流)
最大流求混合图是否存在欧拉回路. 以下内容摘自http://www.cnblogs.com/Missa/archive/2012/12/05/2803107.html 讲的很清楚. 混合图的欧拉回路问 ...
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Sub ...
- poj1637Sightseeing tour(混合图欧拉回路)
题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...
- 混合图欧拉回路POJ1637Sightseeing tour
http://www.cnblogs.com/looker_acm/archive/2010/08/15/1799919.html /* ** 混合图欧拉回路 ** 只记录各定点的出度与入度之差,有向 ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- Euler Tour Tree与dynamic connectivity
Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...
- POJ2677 Tour[DP 状态规定]
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4307 Accepted: 1894 Description ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
随机推荐
- Eclipse导入的Maven项目没有Build Path
我导入的是 Signal-Server项目到 Eclipse中,发现 src 文件夹上面没有#号,包视图和语法提示都没有 ~~ 解决方法: 修改 Project Facets 在 项目右键 -> ...
- Sublime Text添加gcc编译器
{ "shell_cmd" : "gcc $file_name -o ${file_base_name}", "working_dir" : ...
- 【leetcode】328. Odd Even Linked List
题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- read more阅读更多,文字超过三行字符后面添加省略号
var text;$('.blog-item').each(function (i) {text = $(this).find('.blog-excerpt').html();if (text.len ...
- checklistbox用法
删除:CheckListBox.DeleteSelected; 上下移: CheckListBox.Items.Move 删除用 CheckListBox1.Items.Delete(Index); ...
- window安装reidis完成之后,想要把数据存入redis,必须开扩展,不然报错,redis windows phpstudy 安装扩展
redis windows phpstudy 安装扩展 1.http://windows.php.net/downloads/pecl/releases/redis/3.1.5rc1/ 2.htt ...
- 依赖Anaconda环境安装TensorFlow库,避免采坑
TensorFlow™ 简介: TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示 ...
- 将本地已有的一个项目上传到新建的git仓库的方法
将本地已有的一个非git项目上传到新建的git仓库的方法一共有两种. 一. 克隆+拷贝 第一种方法比较简单,直接用把远程仓库拉到本地,然后再把自己本地的项目拷贝到仓库中去.然后push到远程仓库上去即 ...
- python去除rpm仓库中同名低版本的包
编程思路1 遍历目标路径的rpm包并保存特性包列表: 2 利用python模块rpmUtils提取RPM包的特征信息:包名 版本号 架构 3 遍历特性列表中存在重复包名的rpm, 将低版本的rpm包 ...
- MacOS安装npm全局包的权限问题
MacOS,安装npm全局包提示没有写入权限: npm WARN checkPermissions Missing write access to /usr/local/lib/node_module ...