题目描述

Bobo has a directed graph G with n vertex labeled by 1,2,3,..n.

Let D(i,j) be the number of edges from vertex i to vertex j on the shortest path.

If the shortest path does not exist,then D(i,j)=n.

Bobo would like to find the sum of  D(i,j)*D(i,j) for all 1<=i<=n and 1<=j<=n.

输入

There are no more than 5 test cases.

The first line contains an integer n(1<=n<=1000).

The i-th of the following n lines contains n integers g(i,1),g(i,2),..g(i,n).

If there is an edge from i to j,then g(i,j)=1,otherwise g(i,j)=0;

输出

An integer denotes the sum of D(i,j)*D(i,j) for all 1<=i<=n and 1<=j<=n.

样例输入

3
010
001
100
2
10
01

样例输出

15
8
题意就是求所有D(i,j)*D(i,j)的和。D(i,j)代表i j之间的最短路径。
正常的想法肯定是 bfs求出任意两点之间的最短路径 但这样做的时间复杂度大概n^3 会超时。
得优化。用set维护未访问的点 因为set的删除 插入的操作都是logn 而n最大是1000。所以总体的时间复杂度是 常数*n^2
/* ***********************************************
Author :guanjun
Created Time :2016/3/21 16:44:25
File Name :neu1685.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 1010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int n;
char mp[maxn][maxn];
int vis[maxn];
int dis[maxn];
ll sum=;
set<int>s;
set<int>::iterator it;
void solve(){
cle(dis);
s.clear();
int cnt=;
queue<int>q;
for(int i=;i<=n;i++){
q.push(i);
for(int j=;j<=n;j++){
if(i==j)continue;
if(mp[i][j]=='')dis[j]=,q.push(j);
else s.insert(j);
}
//cout<<"s "<<s.size()<<endl;
while(!q.empty()){
int x=q.front();q.pop();
cnt=;
for(it=s.begin();it!=s.end();it++){
if(mp[x][*it]==''){
q.push(*it);
dis[*it]=dis[x]+;
vis[++cnt]=*it;
}
}
for(int j=;j<=cnt;j++)s.erase(vis[j]);
}
for(int j=;j<=n;j++){
if(i==j)continue;
else{
if(dis[j]>)sum+=dis[j]*dis[j];
else sum+=n*n;
}
}
cle(dis);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n){
sum=;
for(int i=;i<=n;i++){
scanf("%s",mp[i]+);
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
printf("%c%c",mp[i][j],j==n?10:' ');
}*/
solve();
printf("%lld\n",sum);
}
return ;
}
												

NEU 1685: All Pair Shortest Path的更多相关文章

  1. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  2. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  3. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. 847. Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  6. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  7. Proof for Floyd-Warshall's Shortest Path Derivation Algorithm Also Demonstrates the Hierarchical Path Construction Process

    (THIS BLOG WAS ORIGINALLY WRTITTEN IN CHINESE WITH LINK: http://www.cnblogs.com/waytofall/p/3732920. ...

  8. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  9. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

随机推荐

  1. [ZJOI2007]棋盘制作 (单调栈,动态规划)

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个 8 \times 88×8 大小的黑白相间的方阵,对应八八六十四卦, ...

  2. properties类的基本使用方法

    properties类的基本使用方法1.假设有“pp.properties”,内容有       age=22       2.java中用下面方法:   Properties   props   = ...

  3. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  4. 转自CSDN,关于状态机

    有限状态机FSM思想广泛应用于硬件控制电路设计,也是软件上常用的一种处理方法(软件上称为FMM--有限消息机).它把 复杂的控制逻辑分解成有限个稳定状态,在每个状态上判断事件,变连续处理为离散数字处理 ...

  5. luogu P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...

  6. 转 vs2010转vs2008 其他的一样

    如果你使用VS2010的任何版本写代码,那么在VS2008中就不能打开VS2010的解决方案了,为此,通过以下三步就可以解决了一.对于工程名.sln; 1.用你喜欢的编辑器打开sln文件,比如note ...

  7. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  8. IO重定向

    http://tldp.org/LDP/abs/html/io-redirection.html http://mp.weixin.qq.com/s/JMHDutEG4R0hEaXrYPmCGg 每个 ...

  9. websocket笔记

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6742746.html websocket -- 双向通信网 ...

  10. LayUI后台管理与综合示例

    一.LayUI介绍 layui(谐音:类UI) 是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的内在,体 ...