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 ...
随机推荐
- MSP430之section(1)
1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...
- Java_Web之分层架构
当我们把业务处理的代码与JSP代码混在一起,不易于阅读,不易于代码维护,这就需要分层. 分层模式 1.分层模式是最常见的一种架构模式 2.分层模式是很多架构模式的基础 分层 将解决方案的组件分隔到不同 ...
- 三维重建:SLAM的粒度和工程化问题
百度百科的定义.此文引用了其他博客的一些图像,如有侵权,邮件联系删除. 申明一下,SLAM不是一个算法,而是一个工程. 在计算机视觉中, 三维重建是指根据单视图或者多视图的图像重建三维信息的过程. 由 ...
- 【sqli-labs】 less8 GET - Blind - Boolian Based - Single Quotes (基于布尔的单引号GET盲注)
加单引号 没有任何信息输出 加and 页面变化,不正常是没有任何回显 http://localhost/sqli/Less-8/?id=1' and '1'='1 http://localhost/s ...
- weblogic详解
一.简介 WebLogic是美国Oracle公司出品的一个application server,确切的说是一个基于JAVAEE架构的中间件,WebLogic是用于开发.集成.部署和管理大型分布式Web ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
- 死磕itchat源码--core.py
core.py文件中的Core类定义了itchat的所有接口.且,仅仅是定义了接口,全部在component包中实现重构.其用法如下表述: 缺省 源码如下: # -*- encoding: utf-8 ...
- idea+MAVEN项目
一.首先创建一个maven项目 1.依次点击:File->New->Project 2.左侧面板选择maven(不要选择Create from archetype选项),如下图,点击Nex ...
- appium+python,app自动化测试框架
目前正在写一个app的自动化UI测试框架,目录结构如, 脚本还在调试,实现的方法是从excel表格读取测试用例,执行完成后会将结果保存到Excel中. 等待.......
- swift UITableViewCell 中的单选控制样式
我昨天在网上找了一晚上的资料,但是大多都是OC得语法,swift资料实在是太少了,使得我这个刚入门swift的彩笔好不吃力,后面一直各种翻阅资料,终于让我找到了 visibleCells 这个方法,直 ...