Travel
Travel |
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 155 Accepted Submission(s): 68 |
Problem Description
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
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. |
Input
The input contains several test cases.
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. |
Output
Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
|
Sample Input
|
Sample Output
|
Source
2008 Asia Chengdu Regional Contest Online
|
Recommend
lcy
|
- /*
- 题意:给出你n个点,然后m条双向的路。让你输出第i条路毁坏之后,任意两个城市之间最短路的总和,如果i条路毁坏之后
- 城市不在联通,那么就输出INF
- 初步思路:遍历每条边断了,然后dijkstra搞一下最短路,删边操作,只需要记录一下两点之间线路的条数,如果大于两条
- 的话,删完这条边的时候,两点间的权值还是为1,否则,删完之后权值为零
- #超时:剪一下枝,如果两点间有两条或以上的线路的话,就不用重新dijkstra了,直接调用离线计算好的就行了
- #再次超时:最短路写的不行,这里用最短路的效率不如直接用bfs(),因为这里每条路的权值都是1,这样的话bfs的效率会比
- dijkstra高很多。
- #还是超时:.....找不出问题烦,加一个剪枝条件,遍历第一遍的时候,将用到的边标记一下,如果删除的时候,这条边根本
- 没用到,那么这条边删掉是不影响后边结果的
- #继续超时:换成邻接矩阵试试
- #感悟:靠,用邻接矩阵就过了
- */
- #include<bits/stdc++.h>
- #define INF 3000000
- using namespace std;
- int n,m;
- int u[],v[];
- int mapn[][];//存储地图
- int used[][][];//用来表示用到了哪条边
- int dis[];//表示耗费的路径
- int sum[];//记录每个点的最短路
- int vis[];
- vector<int>edge[];
- int res=;
- int dijkstra(int s,bool flag=false){
- memset(vis,,sizeof vis);
- memset(dis,,sizeof dis);
- queue<int>q;
- q.push(s);
- vis[s]=;
- while(!q.empty()){
- int Start=q.front();
- q.pop();
- // if(flag==false)
- // cout<<"s="<<s<<" Start="<<Start<<endl;
- for(int i=;i<edge[Start].size();i++){
- int Next=edge[Start][i];
- if(mapn[Start][Next]==) continue;
- // if(flag==false)
- // cout<<"Start="<<Start<<" Next="<<Next<<endl;
- if(!vis[Next]){//这个点没有遍历过
- dis[Next]=dis[Start]+;
- if(flag==true){
- used[s][Start][Next]=;
- used[s][Next][Start]=;
- }
- q.push(Next);
- vis[Next]=;
- }
- }
- }
- int res=;
- for(int i=;i<=n;i++){
- if(i==s) continue;
- if(dis[i]==){
- return INF;
- }
- res+=dis[i];
- }
- return res;
- }
- void init(){
- memset(mapn,,sizeof mapn);
- memset(used,,sizeof used);
- for(int i=;i<=;i++){
- edge[i].clear();
- }
- }
- int main(){
- // freopen("in.txt","r",stdin);
- while(scanf("%d%d",&n,&m)!=EOF){
- init();
- for(int i=;i<m;i++){
- scanf("%d%d",&u[i],&v[i]);
- edge[u[i]].push_back(v[i]);
- edge[v[i]].push_back(u[i]);
- mapn[u[i]][v[i]]++;
- mapn[v[i]][u[i]]++;
- }//建图
- // for(int i=1;i<=n;i++){
- // for(int j=1;j<=n;j++){
- // cout<<d_mapn[i][j]<<" ";
- // }
- // cout<<endl;
- // }
- int frist=;
- for(int i=;i<=n;i++){
- sum[i]=dijkstra(i,true);
- // cout<<cur<<" ";
- if(sum[i]==INF){
- frist=INF;
- break;
- }
- frist+=sum[i];
- }
- // cout<<endl;
- //cout<<"frist="<<frist<<endl;
- if(frist==INF){
- for(int i=;i<m;i++)
- printf("INF\n");
- continue;
- }
- for(int i=;i<m;i++){//删除第i条边
- int f=;
- res=;
- if(mapn[u[i]][v[i]]>=){
- printf("%d\n",frist);
- }else{
- // cout<<"一条路"<<endl;
- mapn[u[i]][v[i]]=mapn[v[i]][u[i]]=;//删掉这条边
- // for(int j=1;j<=n;j++){
- // for(int k=1;k<=n;k++){
- // cout<<d_mapn[j][k]<<" ";
- // }
- // cout<<endl;
- // }
- for(int j=;j<=n;j++){
- if(used[j][u[i]][v[i]]==){//这条边没有用到
- res+=sum[j];
- continue;
- }
- int cnt=dijkstra(j);
- // cout<<"i="<<i<<" j="<<j<<" "<<cnt<<endl;
- if(cnt==INF){
- f=;
- break;
- }
- res+=cnt;
- }
- if(f==) puts("INF");
- else printf("%d\n",res);
- mapn[u[i]][v[i]]=mapn[v[i]][u[i]]=;
- }
- }
- }
- return ;
- }
Travel的更多相关文章
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- 【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 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
随机推荐
- 在vue中let var 和const 区别
let和const 使用方法: (1).有没有感觉到在 javascript 使用var会发生变量提升,即脚本开始运行时, 变量已经存在了,但是没有值,所以会输出undefined. 而 ...
- 用vue开发一个app(3,三天的成果)
前言 一个vue的demo 源码说明 项目目录说明 . |-- config // 项目开发环境配置 | |-- index.js // 项目打包部署配置 |-- src // 源码目录 | |-- ...
- 工欲善其事,必先利其器之open live writer写作
在博客园学习有一段时间,想想是不是自己也应该开始写点东西,做点总结,更加快速的提升自己. 查看小组/博客园使用帮助 得知目前windows live writer 已经停止更新并推荐安装 open l ...
- javaWeb正则表达式
对于web来说,字符串的处理特别重要,而正则表达式是对字符串处理的利器,在字符过滤,验证方面都能看到她的身影. 今天需要处理一段json字符串,在用String.replaceAll的过程中,遇到了正 ...
- 调用惯例Calling Convention (或者说:调用约定)
调用惯例影响执行效率,参数的传递方式以及栈清除的方式. 调用惯例 参数传递顺序 谁负责清除参数 参数是否使用暂存器 register 从左到右 被调用者 是 pascal 从左到右 被调用者 否 ...
- 洗礼灵魂,修炼python(2)--python安装和配置
安装python和基本配置: python官方下载地址:www.python.org 打开网站,然后下载对应(32位和64位,windows版还是linux版)的版本,你可以选择python3或者2, ...
- STM8学习 无法仿真原因Starting debug session... -> Emulator reset (usb://usb)... ** Connection error (usb://usb): swim error [30200]: st-link connection error
刚调试程序时,STlink总是连不上,一直提示: Starting debug session...-> Emulator reset (usb://usb)...** Connection e ...
- ios实现无限后台任务
需求 我们的app是使用心跳机制来保持用户的登陆状态,这样才能收到服务器发来的消息和命令,但是当app进入后台以后大约3分钟或者10分钟之后app就会被系统挂起,用户就会超时下线,这样就必须保持app ...
- Python实战之int学习笔记及简单练习
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__ ...
- 表空间tablespace,数据文件datafiles,和控制文件control files介绍
https://docs.oracle.com/cd/B19306_01/server.102/b14220/physical.htm#i5919 本文系翻译 表空间tablespace,数据文件da ...