POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA)
题意分析
- 首先给出T和N,T代表边的数量,N代表图中点的数量
- 图中边是双向边,并不清楚是否有重边,我按有重边写的。
- 直接跑spfa,dij,floyd都可以。
- 求1到N的最短路。
代码总览
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#define nmax 1005
#define inf 1e8+7
using namespace std;
int mp[nmax][nmax];
int n,m;
int shortdis[nmax];
void spfa(int s)
{
bool isinqueue[nmax];
queue<int> q;
while(!q.empty()) q.pop();
int now = s;
memset(isinqueue,0,sizeof(isinqueue));
for(int i = 1;i<=n;++i) shortdis[i] = inf;
shortdis[s] = 0;
q.push(s);
isinqueue[s] = true;
while(!q.empty()){
now = q.front(); q.pop();
isinqueue[now] = false;
for(int i = 1;i<=n;++i){
if(i != now && mp[now][i] + shortdis[now] < shortdis[i]){
q.push(i);
isinqueue[i] = true;
shortdis[i] = shortdis[now] + mp[now][i];
}
}
}
}
int main()
{
while(scanf("%d %d",&m,&n) != EOF){
for(int i = 1 ;i<=n;++i)
for(int j = 1;j<=n;++j)
mp[i][j] = inf;
for(int i = 1;i<=m;++i){
int x,y,dis;
scanf("%d %d %d",&x,&y,&dis);
int temp = mp[x][y];
if(dis<temp){
mp[x][y] = mp[y][x] = dis;
}
}
spfa(1);
printf("%d\n",shortdis[n]);
}
return 0;
}
POJ.2387 Til the Cows Come Home (SPFA)的更多相关文章
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
随机推荐
- 「日常训练」Balancing Act(POJ-1655)
题意与分析 树的重心板子题. 值得考虑的是,重心究竟有哪些优秀的性质? 这里是一些网上能看到的性质: (判定性质)找到一个点,其所有的子树中最大的子树节点数最少(子树可以"倒着看" ...
- I Hate It:线段树:单点修改+区间查询
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- tomcat下载、安装
下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 启动脚本:star ...
- 关于css的总结
写在前面 ,学好css,需要长期的推敲和积累 ,细节是不断完善的,逐渐形成自己的风格 让自己的css更加接近优雅. 下面来总结一些我觉得比较好的css代码风格 : 1. 一般网页中的背景 用 ...
- c++ string需要注意的地方
There are multiple answers based on what you are doing with the string. 1) Using the string as an id ...
- MFC修改视图CView的背景颜色
(1) 在CYournameView(就是你的视图类,以下以CDrawLineView为例)添加了一个背景颜色变量 COLORREF m_bgcolor; (2)修改这个函数: BOOL CDrawL ...
- DDB与DIB
DB与DIB的区别是什么?觉得书上介绍的有点抽象.不容易理解.他们两者之间的区别的“物理意义” [“现实意义”]——姑且这么叫吧,呵呵!被这个问题困扰了很久,所以今天决定好好查资料总结一下,把它彻底搞 ...
- JavaScript:理解事件、事件处理函数、钩子函数、回调函数
详情请点击 http://www.jianshu.com/p/a0c580ed3432
- 性能分析Linux服务器CPU利用率(转)
1. 指标范围 1.1 User mode CPU utilization+ System mode CPU utilization 合理值:60-85%,如果在一个多用户系统中us+sy时间超过 ...
- 数字证书认证这点事, SSL/TLS,OpenSSL
1.概念 数字证书 HTTPS请求时,Server发给浏览器的认证数据,用私钥签名,并且告诉浏览器公钥,利用公钥解密签名,确认Server身份. 证书还会指明相应的CA,CA能确认证书是否真的是CA颁 ...