Constructing Roads(SPFA+邻接表)
题目描述
Long long ago, There was a country named X, the country has N cities which are numbered from 1 to N.
The king of Country-X wants to construct some roads.
Please note that Country-X is blessed by an angel. He(The angel is a boy? This is no science, but do not care about those details, this angel is so cute, he must be a boy) can use magic to make one road connections directly from two cities’ cost to be half, but the magic can only be used once.
The king wants to know the minimal cost to construct a road between City-A and City-B. Because of there are so many cities and roads, the construction division comes to you, the only programmer he knows, for help.
You should write a program to calculate the minimal cost between City-A and City-B to help him.
输入
For each test case:
The fist line is two integers N and M (2 <= N <= 1000,0 <= M <= 50000).
Each of the following M lines contains three integers U、V and W (1<= U,V<= N, 0 <= W <= 1000) . It shows that if we construct a road between the U-th city and the V-th city , the cost is W.
The next line is two integers A and B (1<= A, B <= N).
输出
示例输入
2 1
1 2 99
1 2
4 3
1 2 312
2 3 520
3 1 999
3 4
示例输出
49
No solution 题意:给m条边,求出s到t的最短路径,其中有一条边的权值可以减半。
思路:两次SPFA,再穷举每一条边,让其减半,最后比较得住最小权值。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
struct node
{
int u,v,w;
int next;
}edge[];
int n,m,cnt;
int p[];
int dis[][maxn];//dis[0][i]表示起点到所有点的最短路,dis[1][i]表示终点到所有点的最短路。
int inque[maxn]; void add(int u, int v, int w)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = p[u];
p[u] = cnt;
cnt++;
}
void spfa(int s, int f)
{
queue<int>que;
while(!que.empty())
que.pop();
memset(inque,,sizeof(inque));
for(int i = ; i <= n; i++)
dis[f][i] = INF; que.push(s);
inque[s] = ;
dis[f][s] = ; while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; for(int i = p[u]; i; i = edge[i].next)
{
if(dis[f][edge[i].v] > dis[f][u] + edge[i].w)
{
dis[f][edge[i].v] = dis[f][u] + edge[i].w;
if(!inque[edge[i].v])
{
inque[edge[i].v] = ;
que.push(edge[i].v);
}
}
}
}
} int main()
{
//freopen("data1.in","r",stdin);
//freopen("c.txt","w",stdout);
while(~scanf("%d %d",&n,&m))
{
int u,v,w;
cnt = ;
memset(p,,sizeof(p));//前项星
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int s,t;
scanf("%d %d",&s,&t);
spfa(s,);
spfa(t,);
int ans = INF;
for(int i = ; i < cnt; i++)
{
u = edge[i].u;
v = edge[i].v;
w = edge[i].w;
int d = dis[][u] + dis[][v] + w/;
if(ans > d)
ans = d;
}
if(ans >= INF)
printf("No solution\n");
else printf("%d\n",ans);
}
return ;
}
Constructing Roads(SPFA+邻接表)的更多相关文章
- poj 1511(SPFA+邻接表)
题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...
- SPFA&邻接表 PASCAL
题目来自CODE[VS]-->热浪 1557 热浪 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 题目描述 Description 德克萨斯纯朴的民眾们这个 ...
- POJ--3268--Silver Cow Party【SPFA+邻接表】
题意:一些牛要去某一点參加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少须要花费多长时间. 思路:从聚会地点返回,相当于是从某一点到其它各个点的最短路径.从牛的家中走到聚会地点,能够把 ...
- HDU 2544 最短路 SPFA 邻接表 模板
Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以如今他们想 ...
- 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes
https://vjudge.net/contest/66569#problem/F 题意:判断图中是否存在负权回路 首先,介绍图的邻接表存储方式 数据结构:图的存储结构之邻接表 邻接表建图,类似于头 ...
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- POJ - 3255 SPFA+邻接表求次短路径
题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- PKU 1511 Invitation Cards (SPFA+邻接表)
题目链接:点击打开链接 题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就 ...
随机推荐
- winform windowsmediaplayer的属性
首先将C:\WINDOWS\system32下的wmp.dll应用到项目中: WMPLib.WindowsMediaPlayerClass player = new WMPLib.WindowsMed ...
- U3D C#脚本的生命周期
MonoBehaviour是每个脚本的基类. 每个Javascript脚本自动继承MonoBehaviour,使用C#或Boo时,需要显式继承MonoBehaviour. 一开始实例化,直到结束实例被 ...
- 在Android上模拟登录广工正方教务系统查询成绩
这是在博客园里开博以来写的第一篇博客. 因为之前看过很多人都有发过关于模拟登录正方软件获取数据的文章,自己觉得挺好玩的便也去动手一做,开始还以为挺难的,但实际做起来还蛮简单的,当然其中还有些小插曲. ...
- 新安装 wampserver 出现 You don't have permission to access / on this server. 或者访问数据库出现You don't have permission to access /phpmyadmin/ on this server.(解决方法)转
本地搭建wamp,输入http://127.0.0.1访问正常,当输入http://localhost/,apache出现You don't have permission to access/on ...
- Angularjs总结(七) 路由及请求服务等
define(['angular'], function (ng) { 'use strict'; var app = ng.module('index-module', ['ngCookies', ...
- 设置UILabel可变高度(根据文本内容自动适应高度)
@property(nonatomic)UILabel *showLabel; // 计算文本所占高度,计算出来之后设置label的高度 // 第一个参数:字体大小,字体大小/样式影响计算字体的高 ...
- (转)log4j日志级别设置成DEBUG时输出Html代码等问题:
log4j日志级别设置成DEBUG时输出Html代码等问题: 问题: log4j日志级别设置成DEBUG时会输出很多信息,包括一些Html代码 解决方案: log4j的控制是树形,所以在log4j.p ...
- 正则过滤html标签
var html = "<p>好好学习,<br>天天向上</p>"; var re=/<[^>]+>/g; var text ...
- 初试jQuery EasyUI
jQuery EasyUI jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...
- 互联网 免费的WebService接口
winform开发暂告于段落,最近再用webservice写接口,接下来的一段时间应该偏向于此方向. (转)一批的免费webservice接口,没有技术含量,只是写在这里做个记忆 股票行情数据 WEB ...