描述

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

题目来源

SWUST Monthly, 2011.1

题目的意思一开始有点不明白,经过贞贞的指导算有点明白了。

就是从起点到车站和车站到终点的这两段路是要自己付,车站跟车站之间是可以报销的,求最少要付多少钱。

至于为什么理解不了是因为没有考虑到起点和终点可以当一个车站。

用邻接矩阵做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的更多相关文章

  1. TOJ3744(Transportation Costs)

    Transportation Costs   Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submi ...

  2. TOJ 4523 Transportation

    Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...

  3. China Tightens Recycling Import Rules

    China Tightens Recycling Import Rules We have all seen the pictures of cities in China with air poll ...

  4. 每日英语:China Poses Challenge for Coal

    China's appetite for coal, once seemingly unlimited, is starting to wane, and the effects are rippli ...

  5. Link Analysis_2_Application

    US Cities Distribution Network 1.1 Task Description Nodes: Cities with attributes (1) location, (2) ...

  6. 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 ...

  7. TOJ 2233 WTommy's Trouble

    2233.   WTommy's Trouble Time Limit: 2.0 Seconds   Memory Limit: 65536KTotal Runs: 1499   Accepted R ...

  8. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  9. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

随机推荐

  1. ASP.NET jquery-1.9.1 语句

    <script src="Script/jquery-1.9.1.js"></script> <script language="javas ...

  2. logback-记录日志

      一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配 ...

  3. 小程序上传多图片多附件多视频 c#后端

    前言: 最近在研究微信小程序,本人自己是C#写后端的;感觉小程序挺好玩的,就自己研究了一下:刚好今天又给我需求,通过小程序上传多图 然后C#后端保存到服务器: 用NET明白 前端上传需要用到流,然后就 ...

  4. Linux服务器其中一个磁盘满了怎么办?在不做磁盘扩容的情况下,一个软连接就搞定。

    适用环境要求:Linux系统及服务器.有管理员权限.存在多余空间的磁盘例如下图中"/home"在磁盘sda5中与"/"不属于同一块磁盘: 1.首先转移正在使用的 ...

  5. 已经上架的app在AppStore上搜不到的解决办法

    1.问题呈现 相信很多人都遇到过这个问题,天天刷iTunes connect,终于发现app已经上架了,兴奋的跑过去告诉老板,老板说好,大家都装一个吧! 这时候只能一边不慌不忙地甩锅给苹果,一边快马加 ...

  6. 基于CAS的单点登录实战(2)-- 搭建cas的php客户端

    在这之前已经搭好了CAS服务端 基于CAS的单点登录实战(1)-- 搭建cas服务器 PHP-Client php-Client是官方支持的,去官网下个最新版就好了.phpCAS 接入很简单,解压放到 ...

  7. IPython绘图和可视化---matplotlib 入门

    最近总是需要用matplotlib绘制一些图,由于是新手,所以总是需要去翻书来找怎么用,即使刚用过的,也总是忘.所以,想写一个入门的教程,一方面帮助我自己熟悉这些函数,另一方面有比我还小白的新手可以借 ...

  8. window.open()弹出窗口被拦截

    之前有个需求是输入一些配置,然后点击预览,通过接口保存配置并返回预览页面链接,在新页面中打开链接.后来测试一直说没有新页面打开,我一看,原来是被浏览器拦截了. 原因如下: 浏览器只有在认为click和 ...

  9. Flink学习笔记:Connectors概述

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  10. 【重要的css属性学习】看了乙醇的文章,统计了几个高star前端框架下,Css属性出现最多的,这里学习记录一下

    color background-color display margin-left border-color padding max-width margin-bottom width flex 待 ...