Codeforces 543B. Destroying Roads 最短路+暴力
来源:互联网 发布:js怎么取数组的值 编辑:程序博客网 时间:2024/04/27 23:29
最短路+暴力,暴力BFS求任意两点间的短路,然后暴力枚举哪一段是公共的
import java.util.*;import java.math.*;public class CF543B{final int maxn=3100;final int INF=10000;int n,m;int[][] dp = new int[maxn][maxn];int s1,t1,l1;int s2,t2,l2;boolean[] vis;/***************Add_Edge**********************/class Edge{int to,next;}Edge[] edge = new Edge[maxn*maxn];int[] Adj=new int[maxn];int Size=0;void init(){for(int i=0;i<=n+10;i++) Adj[i]=-1;Size=0;}void AddEdge(int u,int v){edge[Size]=new Edge();edge[Size].to=v;edge[Size].next=Adj[u];Adj[u]=Size++;}/***************Add_Edge**********************/void BFS(int x){vis=new boolean[n+10];vis[x]=true;Queue<Integer> q = new LinkedList<Integer>();q.add(Integer.valueOf(x));while(q.isEmpty()==false){int u=q.poll();for(int i=Adj[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(vis[v]==true) continue;vis[v]=true;dp[x][v]=dp[x][u]+1;q.add(v);}}}CF543B(){Scanner in = new Scanner(System.in);n=in.nextInt(); m=in.nextInt();init();for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){dp[i][j]=INF;}dp[i][i]=0;}for(int i=0;i<m;i++){int u,v;u=in.nextInt(); v=in.nextInt();dp[u][v]=dp[v][u]=1;AddEdge(u,v);AddEdge(v,u);}s1=in.nextInt(); t1=in.nextInt(); l1=in.nextInt();s2=in.nextInt(); t2=in.nextInt(); l2=in.nextInt();for(int i=1;i<=n;i++){BFS(i);}if(dp[s1][t1]>l1||dp[s2][t2]>l2){System.out.println(-1);return ;}int ans=m-dp[s1][t1]-dp[s2][t2];for(int m1=1;m1<=n;m1++){for(int m2=1;m2<=n;m2++){if(m1==m2) continue;if(dp[s1][m1]+dp[m1][m2]+dp[t1][m2]<=l1&&dp[s2][m1]+dp[m1][m2]+dp[t2][m2]<=l2)ans=Math.max(ans,m-dp[s1][m1]-dp[s2][m1]-dp[m1][m2]-dp[t1][m2]-dp[t2][m2]);if(dp[t1][m1]+dp[m1][m2]+dp[s1][m2]<=l1&&dp[s2][m1]+dp[m1][m2]+dp[t2][m2]<=l2)ans=Math.max(ans,m-dp[t1][m1]-dp[s2][m1]-dp[m1][m2]-dp[s1][m2]-dp[t2][m2]);}}System.out.println(ans);}public static void main (String[] args) {new CF543B();}}
1 0
- Codeforces 543B. Destroying Roads 最短路+暴力
- CodeForces 543B Destroying Roads(最短路 + 枚举)
- CodeForces 543B Destroying Roads(最短路BFS)
- Codeforces 543B Destroying Roads 【暴力 SPFA】
- 【最短路】 Codeforces Round #302 (Div. 1) B - Destroying Roads
- Codeforces Round #302 (Div. 1)B. Destroying Roads 最短路
- codeforces 544D Destroying Roads (最短路)
- Destroying Roads - CodeForces 543 B
- Codeforces Round #302 (Div. 1) B Destroying Roads(最短路+枚举)
- codeforces #302 Destroying Roads (最短路径+暴力)
- Codeforces Round #302 (Div. 2) D. Destroying Roads(最短路)
- Codeforces Round #302 (Div. 2)D. Destroying Roads-最短路
- CodeForces 544D Destroying Roads(最短路)
- Codeforces544D:Destroying Roads(最短路)
- Codeforces 544D Destroying Roads【思维枚举+最短路】好题~好题!
- CF #302 Div2 D Destroying Roads(最短路)
- Codeforces 666B World Tour 暴力最短路
- codeforces 544 D. Destroying Roads
- 通知里面到底有什么?
- C++/PHP面经回馈学弟学妹(新浪阿里百度同花顺华为)
- 推荐系统常用算法
- 水晶头的两种接法
- win7右键 windows资源管理器已停止完美解决
- Codeforces 543B. Destroying Roads 最短路+暴力
- [BZOJ3531]SDOI2014旅行
- 解决Ubuntu下QtCreator中不能输入中文的问题
- wordcount spark...
- byteBuffer的用法
- 通知和代理有什么区别?
- Linux下获取代码文件名、代码所在行数及日期时间的C程序实现
- Linux上程序调试的基石(1)--ptrace
- 移动IM分析