最短路的变形,使用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的更多相关文章

  1. Lightoj 1002 - Country Roads(prim算法)

    I am going to my home. There are many cities and many bi-directional roads between them. The cities ...

  2. LightOJ 1341 唯一分解定理

    Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld &a ...

  3. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  4. Light oj 1002 Country Roads (Dijkstra)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...

  5. Bestcoder#5 1002

    Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. acm 1002 算法设计

    最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5610 如果杠铃总质量是奇数直接impossible 接着就考验耐心和仔细周全的考虑了.在WA了三次后终于发 ...

  9. 1002. A+B for Polynomials (25)

    题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...

随机推荐

  1. 无线WEP入侵注意事项

    1.工具bt3/bt4+spoonwep2(dep安装包)+U盘/VMwmare+无线网卡(可以笔记本内置)+UltraISO+unetbootin-windows-latest.exe+2.内置网卡 ...

  2. javascript控制子页面对父页面控件操作

    //赋值 window.parent.document.getElementById("partyid_trade_edit").value = data.data.partyid ...

  3. global, $GLOBALS[]

    // global在函数中产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一旦改变了别名的变量指向地址,就会发生一些意外的情况 $a = 10; function test() { gl ...

  4. Windows Server R2服务器 IIS7 部署MVC3网站

    报错:调用 GetProcAddress 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi ...

  5. AVFoundation 初识

    AVFoundation是苹果 OS X系统和 iOS系统中用于处理基于时间的媒体数据的高级Objective-C框架. iOS中 AVFoundation 在整个体系中所处的角色

  6. PHP常见算法-面试篇(2)

    1.顺序查找 思路分析: 从数组的第一个元素开始一个一个向下查找,如果有和目标一致的元素,查找成功:如果到最后一个元素仍没有目标元素,则查找失败. 代码实现: <?php function se ...

  7. MySQL大数据量快速分页实现

    一般刚开始学SQL语句的时候,会这样写 代码如下:  SELECT * FROM table ORDER BY id LIMIT 1000, 10; 但在数据达到百万级的时候,这样写会慢死 代码如下: ...

  8. ASP.NET MVC 3 Razor Views in SharePoint

    http://tqcblog.com/2011/01/22/asp-net-mvc-3-razor-views-in-sharepoint/ ASP.NET MVC 3 has just been r ...

  9. ExtJS4.2学习(17)表单基本输入控件Ext.form.Field(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-11/189.html --------------- ...

  10. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...