2 条题解

  • 1
    @ 2024-4-5 16:35:06
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    struct Cellar {
        int id;
        int mines;
    };
    
    struct Connection {
        int from;
        int to;
    };
    
    // 用于存储地窖及地雷信息
    vector<Cellar> cellars;
    
    // 用于存储地窖之间的连接路径
    vector<Connection> connections;
    
    // 用于标记地窖是否被访问过
    vector<bool> visited;
    
    // 记录挖到的最大地雷数量
    int maxMines = 0;
    
    // 记录挖到最多地雷时的路径
    vector<int> maxPath;
    
    // 深度优先搜索
    void dfs(int current, vector<int>& path, int mines) {
        visited[current] = true;
        path.push_back(current);
    
        if (mines > maxMines) {
            maxMines = mines;
            maxPath = path;
        }
    
        for (const auto& connection : connections) {
            if (connection.from == current && !visited[connection.to]) {
                dfs(connection.to, path, mines + cellars[connection.to].mines);
            }
        }
    
        // 回溯
        visited[current] = false;
        path.pop_back();
    }
    
    int main() {
        int n;
        cin >> n;
    
        cellars.resize(n);
        visited.resize(n, false);
    
        for (int i = 0; i < n; ++i) {
            cin >> cellars[i].mines;
            cellars[i].id = i + 1;
        }
    
        int xi, yi;
        while (true) {
            cin >> xi >> yi;
            if (xi == 0 && yi == 0) break;
            connections.push_back({xi - 1, yi - 1}); // 0-based indexing
        }
    
        vector<int> path;
        for (int i = 0; i < n; ++i) {
            dfs(i, path, cellars[i].mines);
        }
    
        // 输出路径
        for (size_t i = 0; i < maxPath.size(); ++i) {
            cout << maxPath[i] + 1; // 1-based indexing
            if (i < maxPath.size() - 1) cout << "-";
        }
        cout << endl;
    
        // 输出最多能挖到的地雷数量
        cout << maxMines << endl;
    
        return 0;
    }//杨同学的我试了一下不知道为啥是错的,本来没想写题解的,但是都没正确题解欸,还是写了>-<我这个是确实ac了
    
    • -4
      @ 2021-10-7 9:33:09

      有手就行

      #include<bits/stdc++.h>
      using namespace std;
      int n, u, v, ans, st, a[205], f[205];
      struct edge                                //就是单链表
      {
      	int to,nxt;
      }e[205];
      int head[205], ecnt, next[205];
      void add(int x,int y)
      {
      	e[++ecnt].nxt = head[x];
      	e[ecnt].to = y;
      	head[x] = ecnt;
      }
      int dfs(int x)
      {
      	if(f[x])
          {
              return f[x];
          }                                      //搜过就返回f[x]
      	f[x] = a[x];
      	for(int i = head[x]; i; i = e[i].nxt)
      	{
      		int t=dfs(e[i].to);                //记忆化搜索
      		if(f[x] < t + a[x])
      		{
      			f[x] = t + a[x];
      			next[x] = e[i].to;             //记录
      		}
      	}
      	return f[x];
      }
      int main()
      {
      	cin >> n;
      	for(int i = 1; i <= n; i++)
          {
              cin >> a[i];
          }
          while(true)
          {
              cin >> u >> v;
      		if(u == 0 && v == 0)
              {
                  break;
              }
      		add(u, v);
      	}
      	for(int i = 1; i <= n; i++)
      	{
      		if(ans < dfs(i))
      		{
      			ans = dfs(i);
      			st = i;
      		}
      	}
      	while(st)                              //输出路径
      	{
      		cout << st;
      		st = next[st];
      		if(st)
              {
                  cout << "-";
              }
      	}
      	cout << endl;
          cout << ans;
      	return 0;
      }
      
      • 1

      信息

      ID
      274
      时间
      1000ms
      内存
      16MiB
      难度
      4
      标签
      递交数
      58
      已通过
      28
      上传者