Uvalive 4256 (dp), uvalive00006dp

Source: Internet
Author: User

Uvalive 4256 (dp), uvalive00006dp

A: A number ranging from 1 to n forms an undirected connected graph, giving the connectivity, and then giving a sequence of numbers, this sequence requires that neighboring points be equal or directly connected in the graph. You can modify at least several points in the sequence to meet the sequence requirements.

Question: f [I] [j] indicates the minimum modification point of the sequence composed of the first I arrays ending with the number j, then f [I] [j] = min {f [I] [j], f [I-1] [k] + (d [I]! = J)}. In this case, j = k or g [j] [k] = 1. Finally, f [len] [k] selects the maximum value after all the numbers.

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 205;const int INF = 0x3f3f3f3f;int n, m, g[N][N], d[N], f[N][N];int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);memset(g, 0, sizeof(g));int a, b;for (int i = 0; i < m; i++) {scanf("%d%d", &a, &b);g[a][b] = g[b][a] = 1;}scanf("%d", &m);for (int i = 1; i <= m; i++)scanf("%d", &d[i]);memset(f, INF, sizeof(f));for (int i = 1; i <= n; i++)f[1][i] = (i != d[1]);for (int i = 2; i <= m; i++)for (int j = 1; j <= n; j++)for (int k = 1; k <= n; k++)if (j == k || g[j][k])f[i][j] = min(f[i][j], f[i - 1][k] + (j != d[i]));int res = INF;for (int i = 1; i <= n; i++)res = min(res, f[m][i]);printf("%d\n", res);}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.