HDU 2121 Ice_cream’s world II

HDU 2121 Ice_cream’s world II

Problem Description

After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.

Input

Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.

Output

If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
 

Sample Input

  
  
3 1 0 1 1 4 4 0 1 10 0 2 10 1 3 20 2 3 30
 

Sample Output

  
  
impossible 40 0
 

Author

Wiskey
 

Source

HDU 2007-10 Programming Contest_WarmUp

Solution

朱刘算法

这个题目显然是有向图求最小生成树,但是因为没有根节点,所以认为的将所有节点编号+1,从而引入0号根节点

在用朱刘算法求最小生成树的之前,先考虑一下引入的根节点的问题:根节点到每个节点的距离应该为所有边的距离之和再加上1,最后的最小费用应该是用求出来的值减去根节点到每个节点的距离

先考虑找到最小边,其实就是用两点间的距离不断更新到其前驱节点(父节点)的最小距离,同时记录前驱节点,更新到各点距离最小的点(输出时需减去边数)对于存在到期前驱节点的距离为无穷大的点显然不能由根节点遍历到,那么就不存在最小生成树

然后找环,如果该节点通过其父节点能够再次回到自己,那么该节点就在一个环中,将该环全部标记为一个序号,然后不停的递归搜索,直到没有环为止,就求出了最小生成树(break掉)

最后缩点,把环里的所有边的起点和终点都改为其环的编号,并修改环中点到其父节点的距离为减去当前换种距离后的值,对于不在环中的点,按顺序为之标号即可

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 1010
#define LL long long
#define inf 0x7f7f7f7f
using namespace std;

struct edge {
  int from, to, w;
} e[L * L];
int n, m, s, t, c, cnt, sum, in[L], pre[L], mr, vis[L], id[L];

inline void add(int a, int b, int v) {
  e[cnt].from = a, e[cnt].to = b, e[cnt++].w = v;
}

inline int MST(int root, int N, int E) {
  int res = 0;
  while (true) {
    for (int i = 0; i < N; ++i) in[i] = inf;
    memset(pre, -1, sizeof(pre));
    for (int i = 0; i < E; ++i) {
      int u = e[i].from, v = e[i].to, w = e[i].w;
      if (in[v] > w && u != v) {
	in[v] = w, pre[v] = u;//*
	if (u == root) mr = i;
      }
    }
    for (int i = 0; i < N; ++i) {
      if (i == root) continue;
      if (in[i] == inf) return -1;
    }
    int node = 0;
    memset(vis, -1, sizeof(vis));
    memset(id, -1, sizeof (id));
    in[root] = 0;//*
    for (int i = 0; i < N; ++i) {
      res += in[i];
      int v = i;
      while (id[v] == -1 && v != root && vis[v] != i) vis[v] = i, v = pre[v];
      if (id[v] == -1 && v != root){
	for (int u = pre[v]; u != v; u = pre[u]) id[u] = node;
	id[v] = node++;
      }
    }
    if (node == 0) break;
    for (int i = 0; i < N; ++i) if (id[i] == -1) id[i] = node++;
    for (int i = 0; i < E; ++i) {
      int u = e[i].from, v = e[i].to;
      e[i].from = id[u], e[i].to = id[v];
      if (e[i].from != e[i].to) e[i].w -= in[v];
    }
    N = node, root = id[root];//*
  }
  return res;
}

int main() {
  freopen("HDU2121.in", "r", stdin);
  freopen("HDU2121.out", "w", stdout);
  while (scanf("%d %d", &n, &m) != EOF) {
    cnt = 0, sum = 0;//*
    for (int i = 1; i <= m; ++i) {
      scanf("%d %d %d", &s, &t, &c);
      add(s + 1, t + 1, c);
      sum += c;
    }
    sum++;
    for (int i = 1; i <= n; ++i) add(0, i, sum);//*
    int ans = MST(0, n + 1, cnt);
    if (ans == -1 || ans >= sum * 2) printf("impossible\n\n");//*
    else printf("%d %d\n\n", ans - sum, mr - m);//*
  }
  return 0;
}

Summary

具体写题目时的问题,已在代码中标记出,简单统计一下:

1、每组数据输出后,还要输出一个空行

2、不要忘记根节点到每一个节点的距离

3、每一次递归调用时,更新根节点应更新为其对应的环的标号,而不是mr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 HDU4992 求所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了求模 n 意义下的所有原根,我们需要先求出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数求出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于求最大公约数,phi 函数用于求欧拉函数,pow 函数用于快速幂求模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,求出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值