hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
BFS+链表
代码改自某博客
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 2e5+;
const int INF = 0x3f3f3f3f;
const int A=INF;
const int B=;
typedef long long LL;
int h[maxn],e;
LL dis[maxn];
bool vis[maxn];
int start; struct Edge
{
int v,nxt,w;
} E[maxn<<]; void init()
{
e=;
memset(h,-,sizeof h);
memset(vis,false,sizeof vis);
} void add(int u,int v,int w)
{
E[e].v = v;
E[e].w = w;
E[e].nxt = h[u];
h[u] = e++;
} struct QNode
{
int v,c;
QNode(int _v,int _c)
{
v = _v;
c=_c;
}
bool operator < (const QNode &a) const
{
return c>a.c;
}
}; void dijkstra(int n)
{
memset(dis,INF,sizeof dis);
priority_queue<QNode>Q;
dis[] = ;
Q.push(QNode(,));
while(!Q.empty())
{
QNode tmp = Q.top();
Q.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i=h[u]; ~i; i=E[i].nxt)
{
int v = E[i].v;
int w = E[i].w;
if(vis[v]) continue;
if(dis[u]+w<dis[v])
{
dis[v] = dis[u]+w;
Q.push(QNode(v,dis[v]));
}
}
}
} void BFS(int n,LL val)
{
set<int>ta,tb;
queue<int>Q;
Q.push(start);
dis[start] = ,dis[n] = INF;
for(int i=; i<=n; i++){
if(i==start) continue;
ta.insert(i);
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=h[u]; ~i; i=E[i].nxt)
{
int v = E[i].v;
if(!ta.count(v)) continue;
ta.erase(v);
tb.insert(v);
}
for(set<int>::iterator it=ta.begin(); it!=ta.end(); it++)
{
Q.push(*it);
dis[*it] = dis[u] + val;
}
ta.swap(tb);
tb.clear();
}
} int main()
{
int N,M,T;
int u,v;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
scanf("%d%d",&N,&M);
init();
bool flag = false;
for(int i=; i<M; i++)
{
scanf("%d%d",&u,&v);
add(u,v,A);
add(v,u,A); }
scanf("%d",&start);
dijkstra(N);
BFS(N,B);
bool first=;
for(int i=;i<=N;i++)
{
if(i==start) continue;
if(first) printf(" ");
printf("%d",dis[i]);
first=;
}
printf("\n");
}
}
return ;
} /* 1
2 0
1 */
hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路的更多相关文章
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
- hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学
#include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- HDU 5876 Sparse Graph(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph BFS+set删点
Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...
随机推荐
- CodeForces 706B Interesting drink
排序,二分. 将$x$数组从小到大排序,每次询问的时候只要二分一下位置就可以了. #pragma comment(linker, "/STACK:1024000000,1024000000& ...
- 关于Trie Tree简单实现
最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...
- Zeppelin0.6.2使用hive解释器
Zeppelin0.6.2的jdbc Interpreter 配置 1.拷贝hive的配置文件hive-site.xml到zeppelin-0.6.2-bin-all/conf下. 2.进入conf下 ...
- Memcached帮助类
一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP <?xml version="1.0"?> <configuration> ...
- SharePoint:备份和还原
注意事项: 1.备份和还原最好都用PowerShell,用时候用管理中心会遇到错误. 2.备份PowerShell要注意数据库服务器名,要与管理中心的数据库服务器一致,用ip可能会报错. Restor ...
- 简单的jquery ajax文件上传功能
/* * 图片上传 * 注意如果不加processData:false和contentType:false会报错 */ function uploadImage(image) { var imageF ...
- MVC视图路径修改方法
http://wenku.baidu.com/link?url=MwAaKgGevU7hfRuTyCL95ZbJuDsNc4b__jEWisY9GuzAJzEUgEdoj7uQ-wurbYtz1IQj ...
- C++编程技巧(长期更新)
1.数组使用 int* p = new int[5](); // 数组新建并全部初始化为0 等价于: int* p; p = new int[5](); int* q = new int[5]; ...
- Redis字符串类型相关操作命令
string是redis最基本的类型,可以包括任何类型数据,如jpg图片或者序列化对象. 单个value最大上限是1G字节 如果只使用string类型,redis就可以被看做具有持久化特性的memca ...
- JSONObject处理java.util.Date
JSONObject的内容为: {"userId":"A000004FFDCE14","userName":"好好干g" ...