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 非常相似,改变了输入方式之后, 本题实际上更 ...
随机推荐
- 为什么有些异常throw出去需要在函数头用throws声明,一些就不用
throw new IllegalStateException(".");不用在函数头声明throws IllegalStateExceptionthrow new IOExcep ...
- 删除Git服务器文件但是保留本地文件
参考: https://blog.csdn.net/u012804886/article/details/83059315 https://www.cnblogs.com/wfsovereign/p/ ...
- 学不好Linux?我们分析看看正确的学习方法是什么-马哥教育
2018年里,Linux运维的职位数量和平均薪资水平仍然持续了去年的强劲增幅,比很多开发岗位涨的都快.从研究机构的数据来看,Linux职位数量和工资水平涨幅均在IT行业的前五之列,比去年的表现还要好一 ...
- pig常用命令
一.pig: pig提供了一个基于Hadoop的并行地执行数据流处理的引擎.它包含了一种脚本语言,称为Pig Latin.(类似SQL) 二.Pig Latin: 1.注释: 单行:-- 多行:/* ...
- Autofac 控制反转
class Program { static void Main(string[] args) { IContainer container = Init(); Go(container); Cons ...
- How To: set udev rule for setting the disk permission on ASM disks when using multipath on Linux 6.x
在RHEL6.4上安装11gR2的RAC时,使用了MULTIPATH来聚合绑定多路径的磁盘,并且修改磁盘的权限,赋予grid:asmadmin用户和组. 此时,在安装时可以发现磁盘,日志如下 INFO ...
- poj3176-Cow Bowling【dp】
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range ...
- 基于Composer的Laravel扩展包开发工作流
使用场景 在引用第三方包的时候,对第三方包有改动需求,需要将代码放在自己的仓库:并且自己的其他项目也有需求引用自定义的第三方包:甚至自己会发布修改后的第三方包: 读完本文你讲获得: Git Submo ...
- render: h => h(App) $mount 什么意思
初始一个vue.js项目时,常常发现main.js里有如下代码: new Vue({ render: h => h(App) }).$mount('#app') 这什么意思?那我自己做项目怎么改 ...
- odoo 二次开发小记-----不定时更新
一.odoo中 页面上字段变化引起其他字段范围变化-onchange @api.onchange('company_id') def onchange_parent_id(self): return ...