Constructing Roads(spfa)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=;
const int maxm=maxn*maxn;
const long long INF=1LL<<;
int vis[maxn],head[maxn];
int n,m,cnt = ;
struct node
{
int u,v;
long long w;
int next; } edge[maxm]; inline void add(int u,int v,long long w)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void spfa(int s, long long dis[])
{ queue<int>q;
q.push(s);
vis[s] = ;
dis[s] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int j = head[u]; j!=-; j= edge[j].next)
{
int v = edge[j].v;
long long w = edge[j].w;
if (dis[u]+w < dis[v])
{
dis[v] = dis[u]+w;
q.push(v);
vis[v] = ;
}
}
} }
void init()
{ }
int main()
{
int u,v;
long long w;
long long dis1[maxn],dis2[maxn];
while(~scanf("%d %d",&n,&m))
{
for (int i = ; i <= n; i++)
{
vis[i] = ;
dis1[i] = INF;
dis2[i] = INF;
head[i] = -;
}
cnt = ;
for (int i = ; i < m; i++)
{
scanf("%d %d %lld",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int s,e;
long long min=INF;
scanf("%d %d",&s,&e);
spfa(s,dis1);//求出起点到所有点的最短路径
spfa(e,dis2);//求出终点到所有点的最短路径
for (int i = ; i < cnt; i++)//遍历所有的边
{
u = edge[i].u;
v = edge[i].v;
w = edge[i].w;
if (dis1[u]+dis2[v]+w/ < min)//S->u+e->v+w/2即为改变后的权值,找出最小的
min = dis1[u]+dis2[v]+w/;
}
if(min==INF)
{
puts("No solution");
continue;
}
printf("%lld\n",min);
}
return ;
}
Constructing Roads(spfa)的更多相关文章
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads (MST)
Constructing Roads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
随机推荐
- CDR如何使用钢笔工具进行完美抠图?【6·18特惠倒计时!】
不要以为抠图只能在图像处理软件中实现,矢量图形绘制软件CorelDRAW一样可以,而且方法很多,文章介绍使用CDR钢笔工具抠图的方法. 提示说明: 首先说明一下,CDR中的钢笔工具和其他平面设计软件中 ...
- mstsc windows7/10远程桌面身份验证错误 要求的函数不受支持
之前好好的能远程桌面连接到服务器,但是今天来就不能连接上了,并提示:身份验证错误.要求的函数不受支持. 猜想可能是Windows又更新了什么鬼,后面查询资料知道是由于CredSSP加密Oracle修正 ...
- Vue中.sync修饰符
Vue 中 sync的作用 <FatherComponent :a.sync = 'b'><FatherComponent /> 子组件中emit('update:a',... ...
- Opencv学习之路——自己编写的HOG算法
#include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv ...
- docker的容器可视化工具portainer
1.搜索镜像 [root@holly ~]# docker search portainer 2.下载portainer [root@holly ~]# docker pull portainer/p ...
- 请问spfa+stack 和spfa+queue 是什么原理
一个是bfs加迭代 一个是dfs加迭代 请问迭代是什么 就是不断地做,做到没有更优的解为止 或者是不断得做,做到逼近答案为止.. 栈比队列更快更节省空间
- nyoj_111_分数加减法_201311281341
分数加减法 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个C程序,实现两个分数的加减法 输入 输入包含多行数据 每行数据是一个字符串, ...
- Hibernate 之QBC
转自:http://blog.csdn.net/agromach/article/details/1932290 一.Hibernate 中聚合函数的使用 Criteria接口的Projections ...
- hibernate分表保存日志
@Service("accessLogService")@Transactionalpublic class LogMessageServiceImpl extends BaseD ...
- Linux(1):fork函数
ps:每一篇博客不过为了记录学习的过程,并反思总结,如有错误,还望指正. 函数原型:extern __pid_t fork (void) __THROWNL; 该函数包括于头文件unistd.h中. ...