NEU 1685: All Pair Shortest Path
题目描述
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的更多相关文章
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- (中等) 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...
- 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. ...
- 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 ...
- [CF843D]Dynamic Shortest Path
[CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...
随机推荐
- 【2018.10.15】noip模拟赛Day1
题面 wzj的题解 T1 随便搜 #include<bits/stdc++.h> #define ll long long using namespace std; inline int ...
- 送外卖(codevs 2800)
题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份东西,分别送达n个不同的客户的手上.n个不同的客户分别在1~n个编号的城市中.送外卖的从0号城市出发,然后n个城市都要走一 ...
- 【AIM Tech Round 5 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9844152.html B:https://www.cnblogs.com/myx12345/p/9844205.html ...
- Linux下学习王爽老师的汇编语言
坐起来非常容易,找到这条路确实非常曲折,为了后来的同志们不再纠结,特记录如下: 这几天看汇编语言时,很多人都推荐王爽老师的<汇编语言>,老师的书的确写的很好,但是讲的是ms的汇编,但是总不 ...
- python学习之-requests模块基础
安装版本:2.18 模块导入:import requests l 发送请求 发送GET请求: 获取GITHUB的公共时间线 r = requests.get(url='https://api.git ...
- 王垠:完全用Linux工作 (2003)
完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...
- Js 流程控制
流程控制 顺序.分支.循环 顺序结构 代码一行一行从上往下执行并解析 分支结构 if语句 switch语句 if语句 单分支 if(条件表达式){ //语句块 } 含义:当条件表达式为真的时候就执行里 ...
- rm -rf /* 注意
mkdir -p ~/.trash //创建一个目录作为回收站,这里使用的是用户家目录下的.trash目录 alias rm=trash //命令别名 rm改变为trash,通过将rm命令别名值t ...
- 如何绕过Win8、Win10的systemsetting与注册表校验设置默认浏览器
*本文原创作者:浪子_三少,属Freebuf原创奖励计划,未经许可禁止转载 在win7时我们只需修改注册表就能设置默认浏览器,但是win8.win10下不能直接修改的因为同样的注册表项,win8.wi ...
- C标准提前定义宏,调试时加打印非常实用
#include<stdio.h> int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE_ ...