hdu 5889 最短路+裸最小割

题目:

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1646    Accepted Submission(s): 490


Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as  N  towns and  M  roads, and each road has the same length and connects two towns. The town numbered  1  is where general's castle is located, and the town numbered  N  is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the  i -th road requires  wi  units of wood. Because of lacking resources, you need to use as less wood as possible.
 

Input
The first line of input contains an integer  t , then  t  test cases follow.
For each test case, in the first line there are two integers  N(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  denoting an edge between  u  and  v  of barricade cost  w .
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
  
  
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
 

Sample Output
  
  
4
 

Source


分析;

给一个无向图 先跑一遍最短路,然后把最短路上的所有边拉出来新建一个图,在新图上跑最大流/最小割就得到了答案


代码:(暴力杂糅)

#include <bits/stdc++.h>
using namespace std;
const int maxn=1020;
const int maxm=10020;
const int inf=0x7f7f7f7f;

int t,n,m;

typedef pair<__int64,int> p;//<a,b> 起点到b的最短距离为a

struct edge {
	int to;
	int cost;
	int tmp;///注意加一个变量临时记录输入的C
	edge(int tto=0,int ccost=0,int ttmp=0):to(tto),cost(ccost),tmp(ttmp){}
};

int d[maxn];
vector<edge> gg[maxn];

void dijkstra(int s) {
	priority_queue<p,vector<p>,greater<p> >qu;//堆按照p的first(最短距离)排序,小顶堆
	fill(d,d+n+1,inf);
	d[s]=0;
	qu.push(p(0,s));//从起点出发到顶点s的最短距离为0
	while(!qu.empty()) {
		p temp=qu.top();
		qu.pop();
		int tmpv=temp.second;
		if(temp.first>d[tmpv]) continue;//跳过更新过程中入队的非最小值
		for(int i=0;i<gg[tmpv].size();++i) {//遍历该顶点连出的每条边
			edge e=gg[tmpv][i];
			if(d[e.to]>d[tmpv]+e.cost) {
				d[e.to]=d[tmpv]+e.cost;
				qu.push(p(d[e.to],e.to));
			}
		}
	}
}

struct EDGE{
	int to;//终点
	int cap;//容量
	int rev;//反向边
};

vector<EDGE> g[maxn];
int level[maxn];//顶点到源点的距离标号
int iter[maxn];//当前弧 在其之前的边已经没有用了

//向图中增加一条从from到to容量为cap的边
void add_edge(int from,int to,int cap){
    g[from].push_back((EDGE){to,cap,g[to].size()});
    g[to].push_back((EDGE){from,0,g[from].size() - 1});
}

//通过bfs记算从源点出发的距离标号
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front(); que.pop();
        for(int i=0;i<g[v].size();i++){
            EDGE &e=g[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

//通过dfs寻找增广路
int dfs(int v,int t,int f){
    if(v==t) return f;
    for(int &i=iter[v];i<g[v].size();i++){
        EDGE &e=g[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                g[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow = 0;
    for(;;){
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0){
            flow+=f;
        }
    }
}

int main(){///15MS	2140K
	int a,b,c;
	scanf("%d",&t);
	while(t--){
        scanf("%d%d",&n,&m);
		for(int i=0;i<=n;++i) g[i].clear(),gg[i].clear();
		for(int i=0;i<m;++i){
			scanf("%d%d%d",&a,&b,&c);
			gg[a].push_back(edge(b,1,c));///边权为1 临时记录下c
			gg[b].push_back(edge(a,1,c));
		}
		dijkstra(1);
		for(int i=1;i<=n;++i){
            for(int j=0;j<gg[i].size();++j){
                edge e=gg[i][j];
                if(d[i]+e.cost==d[e.to]){
                    ///cout<<i<<"  "<<e.to<<"  "<<e.cost<<endl;
                    add_edge(i,e.to,e.tmp);
                }
            }
		}
		printf("%d\n",max_flow(1,n));
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值