题目描述

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. oracle 11g 报错记录

    1.ORA-01034: ORACLE not available sqlplus "sys/password as sysdba" 2.ORA-00119: invalid sp ...

  2. angularJs ionic phoneGap 分享

    由于坑较多 就如“天下难事,必作于易吧” 最近有机会接触到了git  node angularJs ionic phoneGap 很开森也很痛苦 分享如下 推荐的几个博客地址: ionic开发插件之n ...

  3. Android Studio 2.2 HTTP proxy setting 提示异常

    操作系统 :MacOS 10.11.6 IDE :Android Studio 2.2 Java Version :1.8 异常现象描述: 在给Android Studio 2.2设置代理时,出现警告 ...

  4. SQL查询一些浅薄的结论

    一些简单的测试结论 在本机经过一些简单的测试,记录数6W条,得出以下结论,不同的硬件环境和数据记录数,可能会有不一样的结论 1.in, or, exists, like, not in , not e ...

  5. SQL分页语句总结

    今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了. 使用top选项 selecttop10*from Orders a where a.orderid notin(s ...

  6. Hibernate session flush

    最近做项目时,用到了hibernnate,批量删除10000条数据时,删除时前台将id传到后台,用in匹配去删除,页面直接卡死. 解决方法,将传过来的10000条id分批删除,每删除五百条后,调用ge ...

  7. 非常的奇葩,终于解决了硬盘从盘盘符消失的问题 http://bbs.gamersky.com/thread-1712710-1-1.html (出处: 游民星空论坛)

    本人用电脑也十多年了,硬盘的问题也碰到过不少.但最近却碰到了一个很奇葩的问题.就是安装了一块全新的SSD硬盘当从盘,但在装上之后,在我的电脑中却不显示,没有盘符.不过打开系统磁盘管理却能显示硬盘信息. ...

  8. XMPPFramework ios 例子中链接服务器失败,opnefire 服务器链接失败

    首先说下上周又做了几天得无用功, 之前一直用的是ejabberd ,这次换了opnefire,有人说opnefire跟新的xmpp协议不兼容,后来又更换成了ejabberd, Github 上得dem ...

  9. c#基础-----数据类型,转义字符,引用类型,类型转换

    数据类型,转义字符,引用类型,类型转换 百度一下

  10. 【html】【0】开始的序言

    人生总得做点什么才显得有意义,在牛逼的梦想也抵挡不住你傻逼似的坚持! 1>本系列适用于没有任何计算机语言基础的小白入门级教程 2>为了我喜欢的一个女生小娜娜 3>为自己系统的学习ht ...