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, 后来想了半天才知道哪里有遗漏.最大的问题 ...
随机推荐
- js任意位数求和
<script> //任意位数求和 function sum(){ if(arguments.length==1) { console.log(arguments[0]) return; ...
- android IntentService和ResultReceiver的异步处理
IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...
- python的requests库怎么发送带cookies的请求
背景: 在用robot做接口自动化时,有一个查询接口需要用到登录后返回的token等信息作为cookies作为参数一起请求(token是在返回体中,并不在cookies中), 刚好create se ...
- 百度地图离线API及地图数据下载工具
全面介绍,请看下列介绍地址,改写目前最新版本的百度V2.0地图,已全面实现离线操作,能到达在线功能的95%以上 http://api.jjszd.com:8081/apituiguang/gistg. ...
- NPOI 2.1.3.1导入Excel
引入NPOI 2.1.3.1的包 项目引入 using NPOI.XSSF.UserModel;using NPOI.SS.UserModel; 控制器方法: public ActionResult ...
- [日常]无线鼠标滚动缩放EXCEL表时,缩放比例过大问题
这也是一个奇葩问题,解决方法: 把USB接收器拔掉重新插上,效果拔群
- 「CF932E」 Team Work
题目链接 戳我 题意 有\(n\)个箱子,从中选出\(i\)个箱子,然后把\(k\)个不同的球装在这\(i\)个箱子中,求方案数. 实际上这题就是求: \[ \sum_{i=1}^nc_n^i*i^k ...
- 新增扩展程序功能打包提交新版 WARNING ITMS-90473 警告问题
1.问题描述 自从在主应用中加入SiriShortCut功能之后,打包程序上传至 iTunes Connect 就会出现警告,看其原因描述是CFBundleVersion主应用与子应用的不一致导致的 ...
- 「BZOJ 2809」「APIO 2012」Dispatching「启发式合并」
题意 给定一个\(1\)为根的树,每个点有\(c,w\)两个属性,你需要从某个点\(u\)子树里选择\(k\)个点,满足选出来的点\(\sum_{i=1}^k w(i)\leq m\),最大化\(k\ ...
- php代码审计6审计xss漏洞
跨站脚本攻击(Cross Site Scripting)是指攻击者利用网站程序对用户输入过滤不足,输入可以显示在页面上对其他用户造成影响的html代码,从而盗取用户资料,利用用户身份进行某种动作或者对 ...