TOJ3744(Transportation Costs)
Transportation Costs

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
#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)的更多相关文章
- 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)
[热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...
- (转)看穿机器学习(W-GAN模型)的黑箱
本文转自:http://www.360doc.com/content/17/0212/11/35919193_628410589.shtml# 看穿机器学习(W-GAN模型)的黑箱 201 ...
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- UVALive 4987---Evacuation Plan(区间DP)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- .net架构设计读书笔记--第三章 第9节 域模型实现(ImplementingDomain Model)
我们长时间争论什么方案是实现域业务领域层架构的最佳方法.最后,我们用一个在线商店案例来说明,其中忽略了许多之前遇到的一些场景.在线商店对很多人来说更容易理解. 一.在线商店项目简介 1. 用例 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- 基于Web的企业网和互联网的信息和应用( 1194.22 )
基于Web的企业网和互联网的信息和应用( 1194.22 ) 原文更新日期: 2001年6月21日原文地址: http://www.access-board.gov/sec508/guide/1194 ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- (原+转)ROC曲线
转自:http://baike.baidu.com/link?url=_H9luL0R0BSz8Lz7aY1Q_hew3JF1w-Zj_a51ggHFB_VYQljACH01pSU_VJtSGrGJO ...
随机推荐
- js实现中文简繁切换效果
html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 编译hadoop版的hello,world
cd ~/src mkdir classes javac -classpath ~/hadoop-/hadoop--core.jar WordCount.java -d classes jar -cv ...
- 邮件协议POP3/IMAP/SMTP服务的区别
2016年09月09日 09時51分 wanglinqiang整理 通过网上查找的资料和自己的总结完成了下面的文章,看完之后相信大家对这三种协议会有更深入的理解.如有错误的地方望指正. POP3 PO ...
- GetSystemMetrics() 函数的用法
可以用GetSystemMetrics函数可以获取系统分辨率,但这只是其功能之一,GetSystemMetrics函数只有一个参数,称之为「索引」,这个索引有75个标识符,通过设置不同的标识符就可以获 ...
- Match & Catch
Codeforces Round #244 (Div. 2) D:http://codeforces.com/contest/427/problem/D 题意:给你两个串,让你找一个最小的串,并且这个 ...
- 【UVA1633】禁止的回文串(状压DP)
题意: 输入正整数n和k(1<=n<=400,1<=k<=10),求长度为n的01串中有多少个不含长度至少为k的回文连续子串.例如,n=k=3时只有4个串满足条件:001,01 ...
- JqueryUI 为什么TypeError: $(...).slides is not a function
单独写一个html发现一切没有问题,但放在自己的网页中作为一部分却出现了问题,最后发现是那些js文件引入顺序出现了问题,
- SQL Server 阻塞分析
一.加锁(locking).阻塞(blocking).死锁(deadlock)定义 加锁:用于管理多个连接的进程.当连接需要访问一块数据时,在这些数据上放置某种类型的锁. 阻塞 ...
- BZOJ1606: [Usaco2008 Dec]Hay For Sale 购买干草
1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 612 Solved: 46 ...
- -_-#flash播放器自适应
设置断点,几个断点下的固定布局