hdu 4034 Graph(逆向floyd)】的更多相关文章

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4034 Problem Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexe…
floyd的松弛部分是 g[i][j] = min(g[i][j], g[i][k] + g[k][j]);也就是说,g[i][j] <= g[i][k] + g[k][j] (存在i->j, i->k, k->j的边). 那么这个题很明显要逆向思考floyd算法.对于新图i,j,k,如果g[i][j] >  g[i][k] + g[k][j],那么肯定是不合理的.而如果g[i][j] =  g[i][k] + g[k][j],明显i->j的边可以删去. //#prag…
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floyd得到的,那么如果从u到v没有小于等于边(u,v)的路径,那么边(u,v)就是必须的,否则从u到v需要走更远的路.如果有路径和边(u,v)是一样的,那么边(u,v)就是不需要的,这是因为,任何需要从u到v的路径都可以用另外一条代替.如果有小于边(u,v)的,那么这就是个非法的最短路表. 代码: #i…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解,输出"impossible". 题解: 因为在floyd中: if(dis[i][k] + dis[k][j] < dis[i][j]) dis[i][j] = dis[i][k] + dis[k][j]; 所以对于原图再跑一遍floyd. 如果出现dis[i][k] + dis[…
Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1927    Accepted Submission(s): 965 Problem Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the…
题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ; const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大 bool vis[MAXN]; int pre[MAXN], cost[MAXN…
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k][j]  那i j这条边就可以不要, 如果dp[i][j] > dp[i][k]+dp[k][j], 那么就无法构图. 以防万一我又加了个vis数组, 是防止i, j这条边减多次的,  我也不知道有没有用== #include <iostream> #include <vector&g…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第三组样例: 题目分析: 判断 a[i][j]==a[i][k]+a[k][j](详看代码) 代码: #include<bits/stdc++.h> using namespace std; ][]; int main() { #ifndef ONLINE_JUDGE freopen("i…
http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2058    Accepted Submission(s): 1030 Problem Description Everyone knows how to calculate the…
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer v…
比赛时才发现自己基础算法都忘得光光了,逆向floyd i->j经过k点,只要i到j的距离大于或者等于,就把这边标记,实为去掉...此时整个图就减一条边 #include<iostream> #include<cstdio> #include<limits.h> #include<memory.h> using namespace std; #define INF INT_MAX #define maxn 110 int n; int d[maxn][m…
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct Node { Node *ch[2]; int r; int v; int s; Node(int v): v(v) { ch[0] = ch[1] = NULL; r = rand(); s…
给出一个最短路邻接矩阵,求出构图的最小边数 正常的floyd的k放在最外面是为了防止i到j的距离被提前确定,而逆向的floyd,i到j的距离已经确定,所以需要在i到j之间枚举k,注意需要break,否则会多删除 Sample Input 3 3 0 1 1 1 0 1 1 1 0 3 0 1 3 4 0 2 7 3 0 3 0 1 4 1 0 2 4 2 0 Sample Output Case 1: 6 Case 2: 4 Case 3: impossible #include<cstdio>…
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路…
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between…
http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是题目给出的n的大小不确定 所以图很稀疏 会有很多孤立点 会多跑很多没用的路径 需要优化一下 不然会超时 我看其他人很多都是用迪杰斯特拉写的,可以试试 AC代码 #include <stdio.h> #include <math.h> #include <string.h>…
http://acm.hdu.edu.cn/showproblem.php?pid=2066 今天复习了一下最短路和最小生成树,发现居然闹了个大笑话-----我居然一直写的是Floyd,但我自己一直以为这是Dijkstra---我居然一直把两个算法 记的是混的,还居然一直没有被别人发现,真是个大乌龙 好了,看看这道题,赤裸裸的最短路水题,首先来个Floyd的,把城市看成点,将连通的的点赋值为给定的时间,未连通的赋值为很大,这是个无向的关系 然后就是三个循环,以一个点为媒介,把每个点遍历一遍,比较…
Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 627    Accepted Submission(s): 204 Problem Description There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1…
Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexes, can you find the original graph?   Input The first line is…
http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始能量有100,行走的途中能量不能小于等于0. 思路: 首先我们用floyd来判断一下1和n之间是否有通路. 其次就是bellman_ford算法来判正环了. #include <iostream> #include <cstring> #include <algorithm>…
Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 285    Accepted Submission(s): 92 Problem Description There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 …
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStatus Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informatics) in GD team. One day, GD team's coach, Prof. G…
题意:给出n个人,m个关系,问是否满足任意两个人之间的距离通过6个人就可以连接 用floyd就可以了,注意距离是大于7 #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #include<set> #include<que…
[题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向图,若干个询问,问从u走k步到达各个顶点的概率. 其中除法化为乘逆元. [思路] 设f[i][j]表示到达i点走了j步的概率,则有转移式: f[i][j]=sigma{ f[pre(i)][j-1]/out[pre(i)] } 其中pre为有向图上的前一个节点,out[u]为u的出度大小. 构造矩阵后使用矩…
思路:将所有的直线的两个端点和城市混在一起,将能直接到达的两个点连线,求一次floyd最短路径.二分枚举bag容量,然后按给的要先后占领的城市由前向后,把能到一步到达的建一条边.然后求一次最小路径覆盖,就是最少需要多少个士兵才能全部占领,跟给出的p值进行比较. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #def…
P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS appeared around the corner shyly and came up with a problem: Given a graph with n nodes and m undirected weighted edges, every node having one of two co…
Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it…
给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<map> using namespace std; #define exp 0.00000001 map<s…
思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility> #include <string> #incl…
Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1467    Accepted Submission(s): 301 Problem Description You are given an undirected graph with N vertexes and M edges. Every ve…