ZOJ-3318
Time Limit: 1 Second Memory Limit: 32768 KB
There are n cities in the dream country. Let's use integers from 1 to n to denote the cities. There are some roads between cities. To some strange, all the roads are bidirectional and the roads change from time to time. You have m maps of the country of different time. You are going to the dream country soon and you want to start your journey at city s and finish it at city t. Though you cannot predict the condition when you get there, you think it is useful to study the maps carefully. After studying the maps, you find that all the roads in all maps have the same length and there is an s-t path in each map. You want to choose an s-t path in each map and the paths are relatively short. Further more you don't want too many changes in the paths.
Formally suppose you have chosen an s-t path in each map, namely P1, P2,...Pm. Let's define a path's length to be simply the number of edges in it and use LEN to denote the total length of all the paths. Let's define a function as follow: CHANGE(P1, P2,...Pm) is the number of indices i (0 < i < m) for which Pi != Pi+1. Let's define the cost function as follow: COST(P1, P2,...Pm) = LEN + CHANGE(P1, P2,...Pm). You are supposed to find the minimum cost.
Input
There are multiple test cases. The first line of input is an integer T (0 < T < 205) indicating the number of test cases. Then T test cases follow. The first line of each test case is 4 integers n, m, s, t (1 < n, m <= 30, 0 < s, t <= n, s != t). Then there are m map descriptions. The first line of each map description is an integer R, the number of roads in the map (0 < R <= n * (n - 1) / 2). Each of the next R lines contains two integers a, b, the two cities that road connects( 0 < a, b <= n, a != b). You can assume that for each test case there is an s-t path in each map.
Output
For each test case, output in a line the minimum cost defined above.
Sample Input
- 2
- 3 3 2 3
- 2
- 1 2
- 3 1
- 3
- 1 2
- 2 3
- 3 2
- 2
- 2 1
- 2 3
- 4 2 1 4
- 3
- 1 2
- 2 3
- 3 4
- 3
- 1 2
- 2 3
- 3 4
Sample Output
- 5
- 6
Hint
Test case 1: three paths are 2-1-3, 2-3, 2-3.
Test case 2: both paths are 1-2-3-4.
Author: CAO, Peng
Source: The 10th Zhejiang University Programming Contest
Submit
- #include <iostream>
- #include <stdio.h>
- #include<cmath>
- #include<algorithm>
- #include<string.h>
- #include<queue>
- #include<set>
- #define maxn 40
- using namespace std;
- int dist[maxn];
- int mark[maxn][maxn];
- int Edge[maxn][maxn];
- int mmap[maxn][maxn][maxn];
- int dp[maxn];
- int n,m,s,t,flag;
- int bfs()
- {
- memset(dist,0x3f,sizeof(dist));
- flag = dist[0];
- dist[s] = 0;
- queue<int>que;
- while(!que.empty()) que.pop();
- que.push(s);
- while(!que.empty())
- {
- int tmp = que.front();
- que.pop();
- for(int i=1;i<=n;i++)
- {
- if(Edge[tmp][i] == 1 && dist[i] > dist[tmp] + 1)
- {
- dist[i] = dist[tmp] + 1;
- que.push(i);
- }
- }
- }
- return dist[t];
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- #endif // ONLINE_JUDGE
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d %d %d %d",&n,&m,&s,&t);
- int R,u,v;
- memset(mmap,0,sizeof(mmap));
- memset(mark,0,sizeof(mark));
- memset(dp,0,sizeof(dp));
- for(int i=0; i<m; i++)
- {
- scanf("%d",&R);
- for(int j=0; j<R; j++)
- {
- scanf("%d %d",&u,&v);
- mmap[i][u][v] = 1;
- mmap[i][v][u] = 1;
- }
- }
- for(int i=0; i<m; i++)
- {
- memset(Edge,1,sizeof(Edge));
- for(int j=i; j<m; j++)
- {
- for(int k=1; k<=n; k++)
- {
- for(int t= 1; t <=n; t++)
- {
- Edge[k][t] = Edge[k][t] & mmap[j][k][t];
- }
- }
- mark[i][j] = bfs();
- }
- }
- dp[0] = mark[0][0];
- for(int i=1;i<=m;i++)
- {
- if(mark[0][i] != flag) dp[i] = mark[0][i] *(i+1);
- else dp[i] = flag;
- for(int j=0;j<i;j++)
- {
- if(mark[j+1][i] != flag)
- dp[i] = min(dp[i] ,dp[j] + mark[j+1][i]*(i-j) + 1);
- }
- }
- printf("%d\n",dp[m-1]);
- }
- return 0;
- }
ZOJ-3318的更多相关文章
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
- ZOJ 1958. Friends
题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...
- ZOJ
某年浙大研究生考试的题目. 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下:1. zoj能AC:2. 若字符串形式为xzojx,则也能AC, ...
随机推荐
- LOJ6276:果树——题解
https://loj.ac/problem/6276#submit_code NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求.这颗果树有N 个节点 ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- MFC:CTime类和CTimeSpan类
CTime类 CTime类表示日期和时间,上限是3000年12月31日,下限是1970年1月1日 12:00:00 AM GMT. CTime(); 构造一个未经初始化的CTime对象.此状态的CTi ...
- 【题解】Huge Mods UVa 10692 欧拉定理
题意:计算a1^( a2^( a3^( a4^( a5^(...) ) ) ) ) % m的值,输入a数组和m,不保证m是质数,不保证互质 裸的欧拉定理题目,考的就一个公式 a^b = a^( b % ...
- bzoj 1131 [POI2008]Sta 树形dp 转移根模板题
[POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1889 Solved: 729[Submit][Status][Discu ...
- JavaScript设计模式学习之单例模式
一.单例模式介绍 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问, ...
- 安卓弹出键盘隐藏fixed定位相关的元素(obj必须是class)
//安卓弹出键盘隐藏fixed定位相关的元素(obj必须是class) function displayFixed(obj){ var h = document.body.scrollHeight; ...
- 页面自适应<meta name="viewport">标签设置
viewport: 它在页面中设置,是应对手机模式访问网站.网页对屏幕而做的一些设置.通常手机浏览器打开页面后,会把页面放在一个虚拟的“窗 口”–这个比窗口大,也就是你常发现页面可以进行拖动.放大放小 ...
- 「模板」AC自动机(ACAM)
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> u ...
- 【BZOJ4837】LRU算法 [模拟]
LRU算法 Time Limit: 6 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 小Q同学在学习操作系统中内存管理的 ...