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. ListView复用和优化详解

    我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...

  2. postgresql 的触发器

    今天编写了一个触发器 功能: 有两个表,当一个表的字段有所改动的时候,另一个表跟着改动 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS T ...

  3. python路径函操作

    #判断是否为文件 os.path.isfile   #判断是否为目录 os.path.isdir   #返回文件名 os.path.basename(path)   #返回文件目录 os.path.d ...

  4. Python Standard Library 学习(一) -- Built-in Functions 内建函数

    内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...

  5. 在IT学习中的“认识论”

    先有“感性认识”,再有“理性认识”.这句经典认识论,在IT领域应用广泛. 一个项目,首先是分析需求,也就是功能层面,这相当于先有“感性认识”:然后才是具体的技术实现,也就是“理性认识”. IT学习分析 ...

  6. 【转】TypeScript中文入门教程

    目录 虽然我是转载的,但看在Copy这么多文章也是很幸苦的好吧,我罗列一个目录. 转载:<TypeScript 中文入门教程> 17.注解 (2015-12-03 11:36) 转载:&l ...

  7. Uva_11427 Expect the Expected

    题目链接 题意: 你玩纸牌, 如果当天晚上你赢的局数比例 大于 p, 就去睡觉, 第二天继续. 如果小于等于p, 就去睡觉, 并且以后都不玩了. 每晚最多玩n局, 每局赢的概率为p , 求玩的天数的期 ...

  8. h.264并行解码算法分析

    并行算法类型可以分为两类 Function-level Decomposition,按照功能模块进行并行 Data-level Decomposition,按照数据划分进行并行 Function-le ...

  9. Java 引用 WeakReference

    Reference 是一个抽象类,而 SoftReference,WeakReference,PhantomReference 以及 FinalReference 都是继承它的具体类.接下来我们来分别 ...

  10. java 中的访问修饰符

    一. public:所有类都可以访问 protected:所有子类和同包下的类都可以访问 缺省:同包类都可以访问 private:类本身才可以访问 注意点:protected修饰类属性时,例如 pac ...