TOJ 3744 Transportation Costs
描述
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?
输入
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.
输出
If they can not reach the destination output -1, otherwise output the minimum cost.
样例输入
5 5
1 2 7
1 3 3
3 4 3
2 5 3
4 5 3
样例输出
3
题目来源
题目的意思一开始有点不明白,经过贞贞的指导算有点明白了。
就是从起点到车站和车站到终点的这两段路是要自己付,车站跟车站之间是可以报销的,求最少要付多少钱。
至于为什么理解不了是因为没有考虑到起点和终点可以当一个车站。
用邻接矩阵做dijkstra一直过不了,后来用spfa才过的,不知道为什么,可能是重边没有处理好。
#include <stdio.h>
#include <math.h>
#include <queue>
#include <vector>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 105
using namespace std;
int n,m;
bool visited[MAXN];
struct Node{
int w;
int to;
};
vector<Node> vec[MAXN];
void addedge(int u, int v, int w){
Node no;
no.to=v;
no.w=w;
vec[u].push_back(no);
}
void spfa(int begin ,int dist[MAXN]){
for (int i=1; i<=n; i++){
dist[i]=inf;
visited[i]=false;
}
queue<Node> Q;
dist[begin] = 0;
visited[begin] = true;
Node now;
now.to=begin;
now.w=0;
Q.push(now);
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
for(int i=0; i<vec[temp.to].size(); i++){
Node t1=vec[temp.to][i];
int v=temp.w + t1.w ;
if(v<dist[t1.to]){
dist[t1.to] =v;
t1.w=v;
Q.push(t1);
}
}
}
}
int main(int argc, char *argv[])
{
int u,v,w;
int dist1[MAXN];
int dist2[MAXN];
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=1; i<=n; i++){
vec[i].clear();
}
for(int i=1; i<=m; i++){
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa(1,dist1);
spfa(n,dist2);
int min=inf;
for(int i=1; i<=n; i++){
int size=vec[i].size();
for(int j=0; j<size; j++){
int to=vec[i][j].to;
if(dist1[i]!=inf && dist2[i]!=inf && dist1[i]+dist2[to]<min)
min=dist1[i]+dist2[to];
}
}
if(min==inf)
printf("-1\n");
else
printf("%d\n",min);
}
return 0;
}
TOJ 3744 Transportation Costs的更多相关文章
- TOJ3744(Transportation Costs)
Transportation Costs Time Limit(Common/Java):2000MS/6000MS Memory Limit:65536KByte Total Submi ...
- TOJ 4523 Transportation
Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...
- China Tightens Recycling Import Rules
China Tightens Recycling Import Rules We have all seen the pictures of cities in China with air poll ...
- 每日英语:China Poses Challenge for Coal
China's appetite for coal, once seemingly unlimited, is starting to wane, and the effects are rippli ...
- Link Analysis_2_Application
US Cities Distribution Network 1.1 Task Description Nodes: Cities with attributes (1) location, (2) ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力
Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...
- TOJ 2233 WTommy's Trouble
2233. WTommy's Trouble Time Limit: 2.0 Seconds Memory Limit: 65536KTotal Runs: 1499 Accepted R ...
- POJ 1797 Heavy Transportation(最大生成树/最短路变形)
传送门 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 31882 Accept ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
随机推荐
- 平台播放声音(ext.js)
首先把需要的两个js文件放在public/core路径下 (文件已经上传到博客了) 音频文件放在文件一级目录 代码:JxCustom.loadAudio("wav/NG.wav") ...
- C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同
一.委托的同步和异步: 1.同步 使用Invoke调用同步,或直接写fun1("func"),在fun1.Invoke这一步会明显的阻塞线程 使用: static void Mai ...
- angular 输入属性
import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-order', templa ...
- mybatis 学习笔记(三):mapper 代理开发 dao 层
mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...
- 局域网内搭建一个服务器,可以使用 https 吗
https://www.v2ex.com/t/472394 这是一个创建于 126 天前的主题,其中的信息可能已经有所发展或是发生改变. 局域网内通过嵌入式设备搭建一个轻量级 web 服务,可以仍然使 ...
- python3入门之列表和元组
获得更多资料欢迎进入我的网站或者 csdn或者博客园 前面一张主要学习了Python的安装,以及第一个程序helloword的编写,以及简单的输入和输出函数,这章主要介绍序列,列表,元组 序列 这章 ...
- postgresql编译安装与调试(一)
因为最近组里的项目和postgresql有关,并且需要查看和调试源码,所以专门学习了一下如何安装和调试postgresql,此博文用来记录自己的安装和调试过程.安装环境是CentOS6(CentOS7 ...
- Reviewing notes 1.1 of Advanced algebra
♦Linear map Definition Linear map A linear map from vector space V to W over a field F is a function ...
- hdp 2.06 安装备忘
1,官方安装说明文档 http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.0.6.0-Win/bk_installing_hdp_for_windo ...
- 小程序tabBar显示问题
我一直在纠结为什么小程序在有的页面显示,有的页面不显示 查了一下才知道,小程序tabBar只在tabBar中的list定义了页面的才会显示,其余页面不会显示 如下例:tabBar只在画红框的页面显示