题目描述

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.

输入

    There are multiple test cases.
    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).

输出

    For each test case,output one line containing the minimal cost , if there is no route from A to B , the output should contain the string “No solution” (without the quotes).

示例输入

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+邻接表)的更多相关文章

  1. poj 1511(SPFA+邻接表)

    题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...

  2. SPFA&邻接表 PASCAL

    题目来自CODE[VS]-->热浪 1557 热浪 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石       题目描述 Description 德克萨斯纯朴的民眾们这个 ...

  3. POJ--3268--Silver Cow Party【SPFA+邻接表】

    题意:一些牛要去某一点參加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少须要花费多长时间. 思路:从聚会地点返回,相当于是从某一点到其它各个点的最短路径.从牛的家中走到聚会地点,能够把 ...

  4. HDU 2544 最短路 SPFA 邻接表 模板

    Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以如今他们想 ...

  5. 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes

    https://vjudge.net/contest/66569#problem/F 题意:判断图中是否存在负权回路 首先,介绍图的邻接表存储方式 数据结构:图的存储结构之邻接表 邻接表建图,类似于头 ...

  6. HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. POJ - 3255 SPFA+邻接表求次短路径

    题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...

  8. Poj(2679),SPFA,邻接表(主流写法)

    题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...

  9. PKU 1511 Invitation Cards (SPFA+邻接表)

    题目链接:点击打开链接 题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就 ...

随机推荐

  1. RedHat7配置IdM server

    IdM服务器是一个集成身份验证服务器. Figure 1.1. The IdM Server: Unifying Services Authentication: Kerberos KDC Kerbe ...

  2. PHP+jQuery+Ajax实现用户登录与退…

    用户登录与退出功能应用在很多地方,而在有些项目中,我们需要使用Ajax方式进行登录,登录成功后只刷新页面局部,从而提升了用户体验度.本文将使用PHP和jQuery来实现登录和退出功能. 查看演示DEM ...

  3. WPF中窗口控件的跨线程调用

    在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...

  4. github 上传或删除 文件 命令

    git clone https://github.com/onionhacker/bananaproxy.git cd ~/../.. git config --global user.email & ...

  5. NSdate 时间格式

    NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y  年 M  年 ...

  6. jQuery 效果方法

    jQuery 效果方法 下面的表格列出了所有用于创建动画效果的 jQuery 方法. 方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被 ...

  7. 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签

    学习jsp不得不学习jsp标签,一般来说,对于一个jsp开发者,可以理解为jsp页面中出现的java代码越少,对jsp的掌握就越好,而替换掉java代码的重要方式就是使用jsp标签.  jsp标签的分 ...

  8. ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...

  9. 时区之痒 - 从手机GPS模块获取的时间,真的是北京时间么?

    去年互联网地图行业开始引入众包模式,国内比较大的地图商,比如四维图新.高德地图.百度地图纷纷开始推出UGC应用,众包给用户采集门址.公交站等信息,并按照工作量给与采集者一定的回报.我曾经玩过某德推出的 ...

  10. C语言运算符与表达式

    1 概论 计算机内存中的数据可以通过变量,常量来表示和存储,那么这些数据如何运算? C语言中提供了大量(34种)的运算符可以用来完成数据的算术,赋值,逻辑,关系,条件判断以及自增自减运算和基于二进制的 ...