lightoj 1002
最短路的变形,使用spfa做。
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<ctime>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define INF 0x7fffffff
#define debug cout << "here" << endl
#define CLR(X, Y) memset(X, Y, sizeof X)
#define FOR(X, Y) for(int i = X;i < Y;i ++)
inline int myMin(int x, int y){return x < y ? x : y;}
inline int myMax(int x, int y){return x < y ? y : x;}
const int MAXN = 505;
int mat[MAXN][MAXN], n;
void bfs(int t){
queue<int>Q;
int dist[MAXN], vis[MAXN];
CLR(dist, 1), CLR(vis, 0);
Q.push(t), dist[t] = 0, vis[t] = 1;
while(!Q.empty()){
int u = Q.front();
vis[u] = 0;
Q.pop();
for(int i = 0;i < n;i ++){
if(mat[u][i] > 0){
if(dist[i] > max(mat[u][i], dist[u])){
dist[i] = max(mat[u][i], dist[u]);
if(!vis[i]){
Q.push(i);
vis[i] = 1;
}
}
}
}
}
for(int i = 0;i < n;i ++){
if(dist[i] > 20000) printf("Impossible\n");
else printf("%d\n", dist[i]);
}
}
int main(int argc, char* argv[]){
int t, m, u, v, w;
#ifndef ONLINE_JUDGE
freopen("in.cpp", "r", stdin);
#endif
scanf("%d", &t);
int tmp = t;
while(t--){
printf("Case %d:\n", tmp-t);
memset(mat, 0, sizeof mat);
scanf("%d%d", &n, &m);
for(int i = 0;i < m;i ++){
scanf("%d%d%d", &u, &v, &w);
if(mat[u][v] == 0) mat[u][v] = mat[v][u] = w;
else mat[u][v] = mat[v][u] = min(mat[u][v], w);
}
scanf("%d", &m);
bfs(m);
}
return 0;
}
lightoj 1002的更多相关文章
- Lightoj 1002 - Country Roads(prim算法)
I am going to my home. There are many cities and many bi-directional roads between them. The cities ...
- LightOJ 1341 唯一分解定理
Aladdin and the Flying Carpet Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld &a ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- Light oj 1002 Country Roads (Dijkstra)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...
- Bestcoder#5 1002
Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- acm 1002 算法设计
最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5610 如果杠铃总质量是奇数直接impossible 接着就考验耐心和仔细周全的考虑了.在WA了三次后终于发 ...
- 1002. A+B for Polynomials (25)
题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...
随机推荐
- 阿里云ECS服务器被DDoS无解,请问我该何去何从?
阿里云ECS服务器被DDoS无解,请问我该何去何从?
- 用Java开发一个本地服务管理软件
一.最终界面先贴上最终效果图,图1为初始化界面,图二为点击启动/停止之后的中间过渡状态,图三为启动成功后弹出的提示框 把动态gif图片嵌入到jpg背景图中?用Adobe ImageReady即可办到 ...
- 51nod1174区间中最大的数
1174 区间中最大的数基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中, ...
- discuz 重新定义jquery的$
最近做个小插件 发现加了这个代码不执行: $.ajax({ url:'plugin.php?id=register:regeist_jiangsu', type:'post', data:{ 'mob ...
- php 时间函数参考
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...
- Java小例子——穷举质数,求平方和,求质因子。
求平方和 public static void main(String[] args) throws IOException { int n; String s; BufferedReader buf ...
- 换行符‘\n’和回车符‘\r’
顾名思义,换行符就是另起一行,回车符就是回到一行的开头,所以我们平时编写文件的回车符应该确切来说叫做回车换行符 '\n' 10 换行(newline)'\r' 13 回车(return) 也可以表示为 ...
- About building ant & install ant on centos7 {ant source code 1.94}
hamcrest-junit-2.0.0.0.jar java-hamcrest-2.0.0.0.jar copy to ant-sourceCodeDir/lib/o ...
- linux awk命令学习
. awk的运行过程 ) awk_script的组成: ① awk_script可以由一条或多条awk_cmd组成,两条awk_cmd之间一般以NEWLINE分隔 ② awk_cmd由两部分组成: a ...
- jQuery CSS 添加/删除类名
addClass(class) — 为每个匹配的元素添加指定的类名.参数 : class — 一个或多个要添加到元素中的CSS类名,请用空格分开(String)示例 一 :为匹配的元素加上 'sele ...