题意:给定一个错误代码,让你修改数据,使得它能够输出正确答案,错误代码是每次取最短的放入。

析:那么我们就可以模拟这个过程,然后修改每条边的权值,使得它能输出正确答案。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2500 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn];
vector<P> before;
bool vis[maxn]; int d[maxn][maxn]; void bfs(int s){
queue<int> q;
q.push(s);
memset(vis, false, sizeof vis); int cnt = 0;
while(!q.empty()){
int u = q.front(); q.pop();
if(vis[u]) continue;
vis[u] = true;
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(vis[v]) continue;
d[u][v] = d[v][u] = ++cnt;
q.push(v);
}
}
} int main(){
int T; cin >> T;
int s;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %d %d", &n, &m, &s);
for(int i = 1; i <= n; ++i) G[i].clear(); int u, v, w;
before.clear();
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &u, &v, &w);
G[u].push_back(v);
G[v].push_back(u);
before.push_back(P(u, v));
} bfs(s);
printf("Case %d:\n", kase);
for(int i = 0; i < before.size(); ++i)
printf("%d %d %d\n", before[i].first, before[i].second, d[before[i].first][before[i].second]);
}
return 0;
}

UVa 12717 Fiasco (BFS模拟)的更多相关文章

  1. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  2. hdu_1495_非常可乐(bfs模拟)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...

  3. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  4. UVa 10603 Fill (BFS && 经典模拟倒水 && 隐式图)

    题意 : 有装满水的6升的杯子.空的3升杯子和1升杯子,3个杯子中都没有刻度.不使用道具情况下,是否可量出4升水呢? 你的任务是解决一般性的问题:设3个杯子的容量分别为a, b, c,最初只有第3个杯 ...

  5. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  6. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  7. BFS 模拟队列(水题)

    BFS 这道题 觉得比较适合BFS新手入门写,也许大家都以为最入门 的BFS题是在二维图上搜索,但是这道题是线性搜索,更加简单 POJ 3278 Catch That Cow Time Limit:  ...

  8. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. UVa 11210 - Chinese Mahjong 模拟, 枚举 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. 【POJ1149&BZOJ1280】PIGS(最大流)

    题意:Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙. 顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪圈的钥匙,他们会将这些猪圈打开并买走固定数目的猪. 所有顾 ...

  2. Codeforces Round #298 (Div. 2) D. Handshakes [贪心]

    传送门 D. Handshakes time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. CDN是什么与CDN加速的原理

    CDN是什么 CDN全称:Content Delivery Network或Content Ddistribute Network,即内容分发网络 CDN设计思路 避让:尽可能避开互联网上有可能影响数 ...

  4. Spring MVC页面重定向实例

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-page-redirection-example.html ...

  5. Excel小tips - 如何设置表格输入数字后末尾自动添加%

    选中一列——鼠标右键——设置单元格格式——数字——自定义——0% 按照以上操作完成后,点击确定,就大功告成了.

  6. How do I know which version of Javascript I'm using?

    Click on this link to see which version your BROWSER is using: http://jsfiddle.net/Ac6CT/ You should ...

  7. Linux 思维导图

    1.Linux学习路径: 2.Linux桌面介绍: 3.FHS(文件系统目录标准): 以上三张图,都是在学习实验楼上的课程--Linux 基础入门,教程里面看到的. 4.Linux需要特别注意的目录: ...

  8. DacningLinks实现

    本文简单分析DancingLinks实现中的数据结构设计,给出了精确覆盖问题及其扩展问题的代码.并应用于数独问题. 先简单描写叙述一下精确覆盖问题: 给定一个N*M的01矩阵,从中选中若干行,这些行向 ...

  9. Office WORD WPS如何设置PPT播放全屏

    1 在设计-页面设置中,幻灯片大小改成自定义,高度和宽度如下图所示.(我个人的笔记本是15.6存的宽屏笔记本,你可以根据自己笔记本的比例修改宽度和高度的数据来或者不同的比例值),注意在HDMI的输出方 ...

  10. Pacemaker 安装与使用

    Pacemaker 仅仅做资源管理器(CRM).底下的消息系统採用 corosync. 安装 以 ubuntu 为例, sudo aptitude install -y pacemaker coros ...