hdu 4034 Graph(逆向floyd)
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的边可以删去。
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define MP make_pair
#define eps 1e-10
using namespace std; const int maxn = 111;
int vis[maxn][maxn], g[maxn][maxn];
int n, T; int reFloyd()
{
int ret = n*(n-1);
REP(k, n) REP(i, n) REP(j, n)
{
if(!vis[i][j] || !vis[i][k] || !vis[k][j]) continue;
else if(g[i][j] > g[i][k] + g[k][j]) return -1;
else if(g[i][j] == g[i][k] + g[k][j])
{
vis[i][j] = 0;
ret--;
}
}
return ret;
} int main()
{
scanf("%d", &T);
FF(kase, 1, T+1)
{
scanf("%d", &n);
REP(i, n) REP(j, n)
{
scanf("%d", &g[i][j]);
vis[i][j] = i == j ? 0 : 1;
}
printf("Case %d: ", kase);
int ans = reFloyd();
if(ans == -1) puts("impossible");
else printf("%d\n", ans);
}
return 0;
}
hdu 4034 Graph(逆向floyd)的更多相关文章
- HDU 4034 Graph(Floyd变形——逆向判断)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4034 Problem Description Everyone knows how to calcu ...
- HDU 4034 Graph Floyd最短路
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...
- HDU 4034 Graph:反向floyd
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...
- hdu 4034 Graph (floyd的深入理解)
Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submi ...
- HDU 4034 Graph(floyd,最短路,简单)
题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...
- hdu 4034 Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...
- Graph(Floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others) Memor ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
随机推荐
- curl多线程类。
<?php /* * Curl 多线程类 * 使用方法: * ======================== $urls = array("http://baidu.com" ...
- VHDL设计时参数定义的方法 例子
-- SPtb LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_ ...
- CentOS和Redhat发行版linux内核版本的对应关系
由于Redhat和CentOS的发行版本现在众多,所以我们应该知道CentOS和Redhat及linux内核之间版本的对应关系对维护系统还是很有帮助的.对应的列表如下: Redhat 9.0————— ...
- Unix/Linux环境C编程入门教程(1) Solaris 11 64bit环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装Solaris 11 64位并且配置好C/C++开发环境 本文所 ...
- MFC渐入渐出框实现方式二
类似360消息弹出框,实现方式一见http://blog.csdn.net/segen_jaa/article/details/7848598. 本文采用另外的API实现渐入渐出效果. 主要API:S ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- iso-开发基础知识-1-程序流程
main-应用程序委托-视图控制器 main()---主函数 应用程序委托 ---AppDelegate 视图控制器 ---ViewController - (BOOL)applicatio ...
- #include <string>
1 append(string T&);字符串拼接 2 c_str string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址. 3 empty() ...
- The Euler function(欧拉函数)
The Euler function Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 布线问题(prime)
布线问题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...