总算今天静下心来学算法。。

Description

Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang. 
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

There are multiple test cases. Please process till EOF. 
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

Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input

8 9
1 2 2
2 3 2
2 4 1
3 5 3
4 5 4
5 8 1
1 6 2
6 7 5
7 8 1
 

Sample Output

2 6
 
题意:给出一幅无向图,n个点m条边。规定 1 为起点,n 为终点。问,若走最短路(可能不止一条),最少删去几条边使得起点与终点不连通,最多删去几条边使得仍然连通。
 
思路:
对于第一个问题,先跑一遍最短路,然后用最短路重新构图。构图方式有二:一是判 d[i]-d[j]==e[i][j],是则加入新图。二是最短路结点的儿子或者父亲,然后依次加入新图。之后,跑一遍最大流求最小割即可。
对于第二个问题,只要在跑最短路的时候,用 num 数组记录维护到当前结点经过的边数即可。初始化num[1]=0;
注意有重边、多边等情况,用邻接表存图。
 
分别用 EdmondsKarp 和 Dinic 做了遍。
前者的复杂度上界为O(nm*m),后者为O(n*nm)。
但大概数据比较和谐,前者也过了。
 
重新构图的时候注意对应点和边。比如最后才找到的bug:
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 ,即得最小割。

最小割中的边数即为第一问的答案。(详情见代码)

对数组设置上界的时候:memset(n_g,0x3f,sizeof(n_g));//每个数都为0x3f3f3f3f。
以下两份代码进一步优化的途径:
1.用数组完成邻接表存图,避免 vector 的 clear 和 push_back;
2.求最短路时,用优先队列优化Dijkstra,或者用spfa算法。
3.用数组模拟队列,这里没有必要模拟环形队列。
其它有待补充。
 
EdmondsKarp:
#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 ;
}
 

最新文章

  1. Peter Hessler和他的中国三部曲(上)
  2. Android 使用js调用Java
  3. SQL Server如何启用xp_cmdshell组件
  4. XP重装之后蓝屏
  5. Codeforces Round #275 Div.1 B Interesting Array --线段树
  6. C#模拟POST登录cnblogs并发布文章
  7. qemu-kvm简单使用
  8. Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台
  9. nginx介绍及安装
  10. 【ASP.NET Web API教程】2.3.4 创建Admin视图
  11. iOS开发之如何修改导航栏的内容
  12. win 10 和 CentOS 7 双系统安装
  13. 发现----Android Demo
  14. ExtJS:Grid数据导出至excel实例
  15. Ubuntu 16——安装——ns2.35和nam
  16. SD Consultant Year End Activities
  17. [Spark][Python]DataFrame中取出有限个记录的例子
  18. centos 安装thrift
  19. mybatis3.2初学感悟
  20. python netifaces usage

热门文章

  1. PyCharm for Mac 调整字体大小
  2. linux配置 yum 源
  3. PAT_A1078#Hashing
  4. HDU 2268 How To Use The Car (数学题)
  5. 51nod1289 大鱼吃小鱼
  6. QT中tableview不能更新数据,why?
  7. ro的session
  8. 使用idea创建maven项目时 需要注意的问题
  9. K大数查询
  10. 解读grub.conf文件