Transportation Costs  

Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte
Total Submit: 129            Accepted: 34

Description

Minya Konka decided to go to Fuzhou to participate in the ACM regional contest at their own expense.Through the efforts, they got a small amount of financial support from their school, but the school could pay only one of those costs between two stations for them.
From SWUST to Fujian Normal University which is the contest organizer, there are a lot of transfer station and there are many routes.For example, you can take the bus to Mianyang Railway Station (airport), then take the train (plane) to Fuzhou,and then take a bus or taxi to the Fujian Normal University.
The school could pay only one of those costs between two stations for them, the others paid by the Minya Konka team members.They want to know what is the minimum cost the need pay.Can you calculate the minimum cost?

Input

There are several test cases.
In each case,the first line has two integers n(n<=100) and m(m<=500),it means there are n stations and m undirected roads.
The next m lines, each line has 3 integers u,v,w,it means there is a undirected road between u and v and it cost w.(1<=u,v<=n,0<=w<=100)
The ID of SWUST is 1,and n is the ID of Fujian Normal University.

Output

If they can not reach the destination output -1, otherwise output the minimum cost.

Sample Input

5 5
1 2 7
1 3 3
3 4 3
2 5 3
4 5 3

Sample Output

3

Source

SWUST Monthly, 2011.1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstring>
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 110; int gn, gm;
vector<pair<int, int> > g[maxn+10];
bool inque[maxn+10];
queue<int> Q; void spfa(int s, int d[]) {
int i;
for(i = 1; i < maxn; i++) d[i] = INF;
d[s] = 0;
while(!Q.empty()) Q.pop();
Q.push(s);
inque[s] = true;
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for(i = 0; i < (int)g[u].size(); i++) {
int t = g[u][i].first;
if(d[u] + g[u][i].second < d[t]) {
d[t] = d[u] + g[u][i].second;
if(!inque[t]) {
inque[t] = true;
Q.push(t);
}
}
}
inque[u] = false;
}
} void init() {
int i;
for(i = 1; i <= gn; i++) {
g[i].clear();
}
} void work(int d1[], int d2[]) {//枚举每一条边.
int i, j;
int mindis = INF;
int x;
for(i = 1; i <= gn; i++) {
for(j = 0; j < (int)g[i].size(); j++) {
x = g[i][j].first;
if(d1[i] != INF && d2[i] != INF && d1[i] + d2[x] < mindis) {
mindis = d1[i] + d2[x];
}
}
}
if(mindis != INF) {
printf("%d\n", mindis);
}
else
printf("-1\n");
} int main()
{
int i;
int u, v, w;
int d1[maxn];
int d2[maxn];
pair<int, int> t;
while(scanf("%d%d", &gn, &gm) != EOF) {
init(); //清空容器.
for(i = 1; i <= gm; i++) {
scanf("%d%d%d", &u, &v, &w);
t.first = v;
t.second = w;
g[u].push_back(t);
t.first = u;
t.second = w;
g[v].push_back(t);
}
spfa(1, d1);//求起点到每个顶点的最短路径.
spfa(gn, d2);//求终点到每个顶点的最短路径.
work(d1, d2);//枚举每条边,求最小值.
}
return 0;
}

TOJ3744(Transportation Costs)的更多相关文章

  1. 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)

    [热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...

  2. (转)看穿机器学习(W-GAN模型)的黑箱

        本文转自:http://www.360doc.com/content/17/0212/11/35919193_628410589.shtml#   看穿机器学习(W-GAN模型)的黑箱 201 ...

  3. 【 UVALive - 5095】Transportation(费用流)

    Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...

  4. UVALive 4987---Evacuation Plan(区间DP)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. .net架构设计读书笔记--第三章 第9节 域模型实现(ImplementingDomain Model)

        我们长时间争论什么方案是实现域业务领域层架构的最佳方法.最后,我们用一个在线商店案例来说明,其中忽略了许多之前遇到的一些场景.在线商店对很多人来说更容易理解. 一.在线商店项目简介 1. 用例 ...

  6. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  7. 基于Web的企业网和互联网的信息和应用( 1194.22 )

    基于Web的企业网和互联网的信息和应用( 1194.22 ) 原文更新日期: 2001年6月21日原文地址: http://www.access-board.gov/sec508/guide/1194 ...

  8. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  9. (原+转)ROC曲线

    转自:http://baike.baidu.com/link?url=_H9luL0R0BSz8Lz7aY1Q_hew3JF1w-Zj_a51ggHFB_VYQljACH01pSU_VJtSGrGJO ...

随机推荐

  1. JS禁止横竖屏切换,强制横竖屏显示

    js判断屏幕横竖屏: function orient() { //alert('gete'); if (window.orientation == 0 || window.orientation == ...

  2. 安装linux系统后要做的事情

    基本安装0 http://www.kali.org.cn/thread-20517-1-1.html 基本安装1 http://defcon.cn/1618.html 基本安装2 http://www ...

  3. codevs 1557 热浪

    传送门 题目描述 Description 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品.Farmer John此时以先天 ...

  4. hdu 1576 A/B 拓展欧几里得算法

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. jQuery Mobile 控制 select 的显示隐藏 display none

    如需要动态控制下拉选择菜单select的显隐,一般考虑使用display:none这个方法. 但在jQueryMobile中的select添加自定义的css,display:none 是无效的. 解决 ...

  6. SolrCloud阶段总结

    http://www.cnblogs.com/guozk/p/3498844.html SolrCloud阶段总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍SolrClou ...

  7. Warm up 2

    hdu4619:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意:题目大意:给你两种纸牌 ,一种水平放置共有n张 ,一种竖直放置共有m张.水平放置的纸 ...

  8. VS2010编译、调用Lua程序

    一) .建立lua源代码工程,编译lua的静态库 1.下载Lua源码 http://www.lua.org/download.html a 下载后解压到一个目录下,这里假设解压到D:\lua-5.1. ...

  9. 【Xamain 跨平台机制原理剖析】

    原文:[Xamain 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原生 ...

  10. Spring MVC 解读——<mvc:annotation-driven/>(转)

    转自:http://my.oschina.net/HeliosFly/blog/205343 Spring MVC 解读——<mvc:annotation-driven/> 一.Annot ...