2015 多校赛 第一场 1007 (hdu 5294)
总算今天静下心来学算法。。
Description
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
Input
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
Output
Sample Input
Sample Output
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
int tmp=d[i]-d[g[i][j]];
if(tmp<) tmp=-tmp;
if(tmp==e[i][j])
addEdge(i,g[i][j],e[i][j]);
}
}
举个例子:i 结点在最短路中前于 j 结点,e[i][j] 表示从 j 到 i 到有向边权值,则 d[i]-d[j]<0,则判了 d[j]-d[i]==e[i][j],错误。这在某些情况下回使得新建的图多了一条边。
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(d[i]+e[i][j]==d[j])
addEdge(i,g[i][j],e[i][j]);
}
}
最小割的定义和求法:
定义:设点集S,T,称将起点在 S 中,终点在 T 中 的所有边删除后将使得 s 无法到达 t 的的集合划分(S,T)称为一个 s-t 割。(s 在 S 中 t 在 T 中)
在求得最大流后,寻找增广路过程中标记的点为集合 S,其余点为 T ,即得最小割。
最小割中的边数即为第一问的答案。(详情见代码)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],bet[maxn],p[maxn];
void init(){
for(int i=;i<=n;i++)
g[i].clear(),e[i].clear(),G[i].clear();
E.clear();
memset(vis,,sizeof(vis));
}
void addEdge(int from,int to,int cap){
E.push_back(Edge(from,to,cap,));
E.push_back(Edge(to,from,,));
M=E.size();
G[from].push_back(M-);
G[to].push_back(M-);
}
void maxflow(int s,int t){
for(;;){
memset(bet,,sizeof(bet));
queue<int>q;
q.push(s);
bet[s]=inf;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(!bet[ee.to]&&ee.cap>ee.flow){
p[ee.to]=G[x][i];
bet[ee.to]=min(bet[x],ee.cap-ee.flow);
q.push(ee.to);
}
}
if(bet[t]) break;
}
if(!bet[t]) break;
for(int u=t;u!=s;u=E[p[u]].from){
E[p[u]].flow+=bet[t];
E[p[u]^].flow-=bet[t];
}
}
}
void Dijk(){
memset(d,0x3f,sizeof(d));
memset(num,0x3f,sizeof(num));
d[]=num[]=;
for(int i=;i<=n;i++){
int u=-;
for(int j=;j<=n;j++) if(!vis[j]){
if(u==-||d[j]<d[u]) u=j;
}
vis[u]=;
for(int j=;j<g[u].size();j++) if(!vis[g[u][j]]){
int tmp=d[u]+e[u][j];
if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+,num[g[u][j]]);
if(tmp<d[g[u][j]]){
d[g[u][j]]=tmp;
num[g[u][j]]=num[u]+;
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b),e[a].push_back(c);
g[b].push_back(a),e[b].push_back(c);
}
Dijk();
int ans1=,ans2=m-num[n];
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(e[i][j]+d[i]==d[g[i][j]])
addEdge(i,g[i][j],e[i][j]);
}
}
maxflow(,n);
for(int i=;i<E.size();i++){
Edge &ee=E[i];
if(bet[ee.from]&&!bet[ee.to]&&ee.cap>) ans1++;
}
printf("%d %d\n",ans1,ans2);
}
return ;
}
以及Dinic:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],cur[maxn],dis[maxn];
void init(){
for(int i=;i<=n;i++)
g[i].clear(),e[i].clear(),G[i].clear();
E.clear();
memset(vis,,sizeof(vis));
}
void addEdge(int from,int to,int cap){
E.push_back(Edge(from,to,cap,));
E.push_back(Edge(to,from,,));
M=E.size();
G[from].push_back(M-);
G[to].push_back(M-);
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push();
dis[]=;
vis[]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(!vis[ee.to]&&ee.cap>ee.flow){
vis[ee.to]=;
dis[ee.to]=dis[x]+;
q.push(ee.to);
}
}
}
return vis[n];
}
int dfs(int x,int a){
if(x==n||a==) return a;
int flow=,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(dis[x]+==dis[ee.to]&& (f=dfs(ee.to,min(a,ee.cap-ee.flow)))>){
ee.flow+=f;
E[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
void maxflow(){
while(bfs()){
memset(cur,,sizeof(cur));
dfs(,inf);
}
}
void Dijk(){
memset(d,0x3f,sizeof(d));
memset(num,0x3f,sizeof(num));
d[]=num[]=;
for(int i=;i<=n;i++){
int u=-;
for(int j=;j<=n;j++) if(!vis[j]){
if(u==-||d[j]<d[u]) u=j;
}
vis[u]=;
for(int j=;j<g[u].size();j++) if(!vis[g[u][j]]){
int tmp=d[u]+e[u][j];
if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+,num[g[u][j]]);
if(tmp<d[g[u][j]]){
d[g[u][j]]=tmp;
num[g[u][j]]=num[u]+;
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b),e[a].push_back(c);
g[b].push_back(a),e[b].push_back(c);
}
Dijk();
int ans1=,ans2=m-num[n];
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(d[i]+e[i][j]==d[g[i][j]])
addEdge(i,g[i][j],e[i][j]);
}
}
maxflow();
for(int i=;i<E.size();i++){
Edge &ee=E[i];
if(ee.from==ee.to) continue;
if(vis[ee.from]&&!vis[ee.to]&&ee.cap>) ans1++;
}
printf("%d %d\n",ans1,ans2);
}
return ;
}
2015 多校赛 第一场 1007 (hdu 5294)的更多相关文章
- 2015 多校赛 第一场 1002 (hdu 5289)
Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...
- 2015 多校赛 第一场 1001 (hdu 5288)
Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...
- hdu5294||2015多校联合第一场1007 最短路+最大流
http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...
- 2015 多校赛 第二场 1006 (hdu 5305)
Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...
- 2015 多校赛 第二场 1004 hdu(5303)
Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...
- 2015 多校赛 第二场 1002 (hdu 5301)
Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2019牛客多校赛第一场 补题 I题
I题 Points Division 题意: 给你n个点,每个点有坐标(xi,yi)和属性(ai,bi),将点集划分为两个集合, 任意 A 集合的点 i 和 B 集合点 j, 不允许 xi > ...
- 2014多校第六场 1007 || HDU 4927 Series 1(杨辉三角组合数)
题目链接 题意 : n个数,每操作一次就变成n-1个数,最后变成一个数,输出这个数,操作是指后一个数减前一个数得到的数写下来. 思路 : 找出几个数,算得时候先不要算出来,用式子代替,例如: 1 2 ...
随机推荐
- 工作中总结的经验之git篇
不要以为你会git,你要知道,git不是只有commit和push 由于系统分析与设计的期末Project需要团队合作开发,因此在这里想谈谈GitHub团队项目合作开发的流程: 项目创建 项目负责人在 ...
- mysql 5.6 中 explicit_defaults_for_timestamp参数
mysql 5.6 中 explicit_defaults_for_timestamp参数 一: 官方文档中关于explicit_defaults_for_timestamp参数说明如下: expli ...
- django分页功能,templatetags的应用
django 将不会将得到的html代码自动转化 from django.utils.html import format_html html =''' <a href='http://www. ...
- Day 18 hashlib,logging模块
hashlib 模块 作用:hash是一种算法,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法,该算法接受传入的内容,经过运算得到一串hash值 特点: 1.只要 ...
- Day 2 语言元素
1.变量和类型 在程序设计中,变量是一种存储数据的载体.计算机中的变量是实际存在的数据或者说是存储器中存储数据的一块内存空间,变量的值可以被读取和修改,这是所有计算和控制的基础.计算机能处理的数据有很 ...
- Python学习笔记之文件处理
1.打开和关闭 要打开文件可以使用函数 open(),它位于自动导入模块 io 中.函数将文件名作为唯一不可缺少的函数,并返回一个文件对象.它还有一个名叫 mode 的参数,用于指定文件模式,其可取值 ...
- 解决@vue/cli 创建项目是安装chromedriver时失败的问题
最近在使用新版vue的命令行工具创建项目时,安装chromedriver老是失败,导致后面的步骤也没有进行.网上搜索了一下,全是使用 工作中常见问题汇总及解决方案 npm install chrome ...
- Problem 25
Problem 25 The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 ...
- Netty学习总结(2)——Netty的高性能架构之道
Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...
- foj 2139
hi.baidu.com/ydlqw/item/120cd21a5afd5becddeeca41?qq-pf-to=pcqq.c2c