Hdu2433 Travel
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3391 Accepted Submission(s): 1162
Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.
The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
The input will be terminated by EOF.
5 1
1 3
3 2
5 4
2 2
1 2
1 2
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7ffffff; int n,m,head[],to[maxn * ],nextt[maxn * ],tot = ,pre[][],num[][];
int d[],vis[],sum[];
bool flag = true; struct node
{
int x,y;
} e[maxn]; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void bfs(int s)
{
queue <int> q;
q.push(s);
for (int i = ; i <= n; i++)
d[i] = inf;
memset(vis,,sizeof(vis));
vis[s] = ;
d[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!vis[v])
{
pre[s][v] = u;
d[v] = d[u] + ;
vis[v] = ;
q.push(v);
}
}
}
for (int i = ; i <= n; i++)
{
if(d[i] == inf)
{
flag = false;
return;
}
else
sum[s] += d[i];
}
} int bfs2(int s)
{
queue <int> q;
q.push(s);
for (int i = ; i <= n; i++)
d[i] = inf;
memset(vis,,sizeof(vis));
vis[s] = ;
d[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!vis[v] && num[u][v])
{
d[v] = d[u] + ;
vis[v] = ;
q.push(v);
}
}
}
int res = ;
for (int i = ; i <= n; i++)
{
if (d[i] == inf)
return -;
else
res += d[i];
}
return res;
} int main()
{
while (scanf("%d%d",&n,&m) != EOF)
{
memset(head,,sizeof(head));
tot = ;
flag = true;
memset(pre,,sizeof(pre));
memset(sum,,sizeof(sum));
memset(num,,sizeof(num));
for (int i = ; i <= m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
num[x][y]++;
num[y][x]++;
e[i].x = x;
e[i].y = y;
add(x,y);
add(y,x);
}
for (int i = ; i <= n; i++)
{
bfs(i);
if(!flag)
break;
}
if (!flag)
{
for (int i = ; i <= m; i++)
puts("INF");
}
else
{
for (int i = ; i <= m; i++)
{
bool flag2 = true;
int ans = ,x = e[i].x,y = e[i].y;
for (int j = ; j <= n; j++)
{
if (pre[j][y] != x && pre[j][x] != y)
{
ans += sum[j];
continue;
}
else
{
num[x][y]--;
num[y][x]--;
int t = bfs2(j);
num[x][y]++;
num[y][x]++;
if (t == -)
{
flag2 = false;
puts("INF");
break;
}
else
ans += t;
}
}
if (flag2)
printf("%d\n",ans);
}
}
} return ;
}
Hdu2433 Travel的更多相关文章
- HDU2433—Travel (BFS,最短路)
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- Linux inode && Fast Directory Travel Method(undone)
目录 . Linux inode简介 . Fast Directory Travel Method 1. Linux inode简介 0x1: 磁盘分割原理 字节 -> 扇区(sector)(每 ...
- HDU - Travel
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- ural 1286. Starship Travel
1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...
- Travel Problem[SZU_K28]
DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...
随机推荐
- 换Mac了,迈入了终端的大门
多终端其实本质和多线程一样,所有终端其实都共享着同一个内存只不过不同终端对共享内存不同部分的权限不同罢了所以对终端的数量必须要有限制 我这里开启了四个线程,很明显四个线程都在跑同一个内存而且四个线程都 ...
- v-for 指令
JS部分: var app = new Vue({ el: "#app", data() { return { list: [1, 2, 3, 4], objList: [ { i ...
- Facebook190亿美元收购WhatsApp
Facebook收购WhatsApp,前后只花费10天时间.这是Facebook迄今规模最大的一笔收购,可能也是史上最昂贵的一笔针对靠私人风投起家的企业的收购案. 2月9日,马克•扎克伯格(Mark ...
- 提升Android ListView性能的几个技巧
ListView如何运作的? ListView是设计应用于对可扩展性和高性能要求的地方.实际上,这就意味着ListView有以下2个要求: 尽可能少的创建View: 只是绘制和布局在屏幕上可见的子Vi ...
- Python Pygame (4) 图像的变换
Pygame中的transform模块可以使得你能够对图像(也就是Surface对象)做各种动作,列如左右上下翻转,按角度转动,放大缩小......,并返回Surface对象.这里列举了transfo ...
- Weighted Median
For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. The weighted median is ...
- c# 捕获一般获取不到的异常
1.主函数入口加异常事件,代码例如: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void M ...
- mysql 多查询临时表的运用
SELECT * from (select count(*) imgCount1 from imagetable where SeriesID = '1201061992020630292018092 ...
- BundleCollection学习(一)
工作中有同事提到了mvc4提供了css,js压缩功能.类BundleCollection所以搜集资料记录学习下. 学习中………… MVC中用 BundleCollection 压缩CSS时图片路径问题 ...
- 201621123037 《Java程序设计》第10周学习总结
作业10-异常 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7 ...