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…
原题链接: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…
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个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解,输出"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[…
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 题目分类:图论 题意: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…
[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…
题目来源: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=1596 find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6911    Accepted Submission(s): 2450 Problem Description XX星球有很多城市,每个城市之间有一条或…
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4430    Accepted Submission(s): 2013 Problem Description Arbitrage is the use of discr…
http://acm.hdu.edu.cn/showproblem.php?pid=1869 六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4355    Accepted Submission(s): 1768 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象…
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665 题意分析:以0为起点,求到Sea的最短路径. 所以可以N为超级汇点,使用floyd求0到N的最短路径. /*Seaside Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1151 Accepted Submission(s): 839 P…
Rank Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1630    Accepted Submission(s): 627 Problem Description there are N ACMers in HDU team.ZJPCPC Sunny Cup 2007 is coming, and lcy want to selec…
六度分离 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为“六度分离”理论(six degrees of separation).虽然米尔格兰姆的理论屡屡应验,一直也有很多社会学家对其兴趣浓厚,但是在30多年的时间里,它从来就没有得到过严谨的证明,只是一种带有传奇色彩的假说而已. Lele对这个理论相当…
题意:给出n条路,起点和终点,问最短距离 用map处理一下地名,再用floyd 可是不懂的是:为什么INF定义成0x7fffffff就输出一堆奇怪的东西,改成100000000就可以了 #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #i…
[题目链接] 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的出度大小. 构造矩阵后使用矩…
虽然题中有n<=100个点,但实际上你必须走过的点只有H<=15个.而且经过任意点但不消耗C[i]跟D[i]可以为无限次,所以可以floyd预处理出H个点的最短路,之后剩下的...就成了裸的TSP了,dp[sta][i]表示已经遍历过了sta集合中的点,现在在i点所需的最少花费.dp[i][j]=-1表示该点不可到达..但是在最后统计最小解的时候要特判... #include<algorithm> #include<iostream> #include<cstri…
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,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前处理,这样我们可以看作是添加点,而且这样的话每次只需要考虑添加点的缩进,所以复杂度是(500)^3,注意,我们每次添加一个点,就给他一个标记,代表这个点已经添加,然后算距离的时候,只有添加过的点才能加上距离 代码: #include <bits/stdc++.h> using namespace…
比赛时才发现自己基础算法都忘得光光了,逆向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…
题意:给几个国家,然后给这些国家之间的汇率.判断能否通过这些汇率差进行套利交易. Floyd的算法可以求出任意两点间的最短路径,最后比较本国与本国的汇率差,如果大于1,则可以.否则不可以. 有向图 一个点到另一点的花费为权值相乘 求乘积的最大值 从点i出发 再回到点i的花费如果大于1 就可以 Sample Input3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFren…
Seaside Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1006    Accepted Submission(s): 722 Problem Description XiaoY is living in a big city, there are N towns in it and some towns near the se…
题目链接:http://codeforces.com/problemset/problem/295/B 题目大意:给出n个点的完全有权有向图,每次删去一个点,求删掉该点之前整张图各个点的最短路之和(包括i->j和j->i).解题思路:这里利用了floyd的性质,下面看一下floyd的写法:for (k=1;k<=n;k++)for (i=1;i<=n;i++)for (j=1;j<=n;j++)a[i][j] = min(a[i][j], a[i][k]+a[k][j]);每…
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…
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 at the beginning. You're also given a sequence of operations and you need to process them as requested. Here's a…