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 ...
随机推荐
- 原生javascript操作class-元素查找-元素是否存在-添加class-移除class
//判断元素是否有classfunction hasClass(ele, cls) { return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\ ...
- PHP获取当前页面完整url地址,包括参数的函数
//php获取当前访问的完整url地址 function get_current_url(){ $current_url='http://'; if(isset($_SERVER['H ...
- phpcms v9指定栏目调用系列教程
调用指定栏目名称: {$CATEGORYS[栏目ID]['catname']} 调用指定栏目url {$CATEGORYS[栏目ID]['url']} 调用指定栏目栏目图片 {$CATEGORYS[栏 ...
- 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
1. VirtualBox的安装 参考常规安装方式即可. VirtualBox 4.3.14 for Windows hosts:http://download.virtualbox.org/virt ...
- ASP.NET MVC轻教程 Step By Step 7——改进Write动作方法
在上一节我们使用强类型视图改进Write视图获得更好的智能感知和代码重构,现在可以进一步的改进动作方法. Step 1. 数据模型绑定 在Save方法中我们使用Request来获取表单传送的值,其实可 ...
- mvc4 to mvc5 and EF5 to EF6
今天把 后台的mvc 升级到了mvc5和ef6 .出错很正常. 下面是一些错误信息. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法 ...
- Matlab norm 用法小记
Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p) Returns sum(abs(A).^p)^(1/p), for ...
- SKProductsRequest ios 7不调用delegate
在iOS7中,内购只能在真机上才会调用 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProdu ...
- c++virtual inline 是否冲突
关于inline关键字:effective c++ item33:明智运用inlining.说到:inline指令就像register指令一样,只是对编译器的一种提示,而不是一个强制命令,意思是编译器 ...
- windows版爬取csdn
use LWP::UserAgent; use POSIX; use HTML::TreeBuilder::XPath; use Encode; use HTML::TreeBuilder; open ...