描述

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. IOException while loading persisted sessions: java.io.EOFException

    运行eclipse启动服务器的时候,出现了IOException while loading persisted sessions: java.io.EOFException报错.本以为是代码修改出现 ...

  2. 国外物联网平台(8):Telit

    国外物联网平台(8) ——Telit 马智 定位 We Bring IoT to Life Telit提供世界上最全面的高性能物联网模块.连接服务和软件. 产品体系 模块 Telit提供丰富专业的物联 ...

  3. xp 专业版组策略只有系统组件

    想要不显示任务栏的提示消息,需要在组策略里面设置,(在"开始→运行"中输入"GPEDIT.MSC"打开组策略,然后依次选择"用户配置→管理模板→任务栏 ...

  4. js 去空格 和 获得字节数

    function removespace(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } function getLength ...

  5. 【C#】特性标签中的属性解释

    第一个为特性作用于类,或者接口(interface) 第二个为是否允许重叠定义,就是连续写两个特性标签 第三个为是否继承,当继承时候,除输出子类外,父类也将输出

  6. WinForm中的多线程

    使用BeginInvoke或Invoke 作用 在自己创建的非UI线程中,进行UI操作,比如更新UI上控件的状态. Windows 窗体中的控件被绑定到特定的线程,不具备线程安全性.因此,如果从另一个 ...

  7. mysql 复制(主从复制)

    一.概述 让一台服务器的数据与其他服务器数据保持同步.一台主库的数据可以同步到多台备库上,而备库本身也可以配置成其他服务器的主库. 主要应用: 1) 数据分布 2) 负载均衡 3) 伪备份.在备份基础 ...

  8. 数据库处理session类

    <?php /* * 使用数据库处理session * php.ini 中 session.save_handler 设为 "user" */ class Dbsession ...

  9. 自定义类型转换器 及 使用 ServletAPI 对象作为方法参数

    自定义类型转换器使用场景: jsp 代码:  <!-- 特殊情况之:类型转换问题 --> <a href="account/deleteAccount?date=2018- ...

  10. 大数据处理框架之Strom:事务

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 apache-flume-1.6.0 ...