Design Tutorial: Inverse the Problem CodeForces
来源:互联网 发布:sql case when isnull 编辑:程序博客网 时间:2024/06/07 13:29
一开始的思路并不是建一颗最小生成树,只是想是否能通过其他的边,然后建立出来的树对应的路径是否和矩阵对应的距离相同,或来想到了可以用最小生成树,然后对于每个起点dfs都更新一遍距离,就可以。其实也可以不用最小生成树可以模拟一边最小生成树prim算法,这是一个思维题目,是一个好题
#include <iostream>#include <algorithm>#include <queue>#include <cstring>#include <cstdio>using namespace std;const int INF = 0x7fffffff;const int MAX = 2000 + 10;int n,m;int num;int p[MAX];int dist[2010][2010];int graph[MAX][MAX];int ans[MAX][MAX];int dis[MAX];int vis[MAX];vector<int> g[MAX];struct Edge //ԭʼͼ{ int from; int to; int w; Edge() {} Edge(int a,int b,int c):from(a),to(b),w(c) {}} e[MAX*MAX];vector<Edge> G[MAX];bool cmp(const Edge &a, const Edge &b){ return a.w < b.w;}void makeSet(){ for(int i = 0; i <= n+10; i++) { p[i] = i; }}int findSet(int x){ if(x != p[x]) p[x] = findSet(p[x]); return p[x];}void addEdge(int from, int to, int w){ G[from].push_back(Edge(from,to,w)); G[to].push_back(Edge(to,from,w));}void dfs(int u,int fa,int v){ if(fa == -1) dis[u] = 0; else dis[u] = dis[fa] + v; for(int i = 0; i < G[u].size(); i++) { Edge tmp = G[u][i]; int to = tmp.to; int cost = tmp.w; if(vis[to]) continue; vis[to] = 1; dfs(to,u,cost); }}int kruscal(){ for(int i = 1; i <= n+5; i++) { for(int j = 1; j <= n+5; j++) { dist[i][j] = INF; } } int i,j; int x, y; int edgeNum = 0; int result = 0; makeSet(); std::sort(e,e+m,cmp); for(i = 0; i < m; i++) { x = findSet(e[i].from); y = findSet(e[i].to); if(x != y) { edgeNum++; addEdge(e[i].from,e[i].to,e[i].w); p[x] = y; result += e[i].w; } if(edgeNum == n - 1) break; } return result;}int main(){ memset(vis,0,sizeof(vis)); int i,j; int num; scanf("%d",&num); int flag1 = 1; m = 0; n = num; for(int i = 1; i <=num; i++) { for(int j = 1; j <= num; j++) { scanf("%d",&graph[i][j]); if(i == j && graph[i][j]) { flag1 = 0; } else if(i > j && graph[i][j] != graph[j][i]) { flag1 = 0; } else if(i != j && !graph[i][j]) { flag1 = 0; } else if(i < j && graph[i][j]) { e[m].from = i; e[m].to = j; e[m].w = graph[i][j]; m++; } } } num = 0; if(flag1 == 0) { puts("NO"); } else { int flag = 1; int mst = kruscal(); for(int i = 1; i <= n; i++) { memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); vis[i] = 1; dfs(i,-1,0); for(int j = 1; j <= n; j++) { if(i < j) { if(graph[i][j] != dis[j]) { flag = 0; break; } } } } if(flag) puts("YES"); else puts("NO"); } return 0;}
阅读全文
1 0
- 【CODEFORCES】 D. Design Tutorial: Inverse the Problem
- Design Tutorial: Inverse the Problem CodeForces
- 【codeforces】 472D Design Tutorial: Inverse the Problem
- Codeforces Round #270 D. Design Tutorial: Inverse the Problem
- Codeforces Round #270 D. Design Tutorial: Inverse the Problem
- Codeforces 472D. Design Tutorial: Inverse the Problem
- Codeforces Round #270-D. Design Tutorial: Inverse the Problem(Prime)
- Design Tutorial: Inverse the Problem cf 270
- cf472D Design Tutorial: Inverse the Problem
- cf472D Design Tutorial: Inverse the Problem
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树)
- Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树+lca)
- codeforces472D Design Tutorial: Inverse the Problem 最小生成树+dfs
- Codeforces 472D. Design Tutorial: Inverse the Problem(一种逆向判定树成立的办法,从Kruskal到dfs)
- Codeforces 472G Design Tutorial: Increase the Constraints(分块+FFT)
- Codeforces Round #270 F Design Tutorial: Change the Goal 高斯消元
- Struts2笔记之自定义封装请求参数的数据类型转换器
- js和jquery在操作dom节点时各自的方法
- ssh移植到Android
- 使用apache的poi实现导入导出excel
- edtools.rar下载和安装
- Design Tutorial: Inverse the Problem CodeForces
- testng源码阅读之多线程包
- 小谜题5
- 汇编(六)
- Java LinkedList
- Egret-H5游戏开发之性能优化总结
- 下拉刷新html
- [arc066e]Addition and Subtraction Hard
- 记录篇,自己在项目中使用过的。