Problem I 成都路口(最短路SPFA算法队列+vector实现)

本文介绍了一种使用SPFA算法解决最短路径问题的方法,并通过一个具体的实现案例详细讲解了如何利用vector数组存储图中各节点的连接关系,进而进行广度优先搜索求解最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

emm......还是一个蓝白点的思想,也是看了别人的题解才知道用vecor数组来表示某点所连接的所有点,vector用的确实好。

#include<bits/stdc++.h>
using namespace std;
int w[109][109];
int dis[109];
vector <int> a[109];
bool vis[109];
int spfa(int s)
{
   queue <int > Q;
   Q.push(s);
   vis[s]=1;
   while(!Q.empty())
   {
       int p=Q.front();
       Q.pop();
       vis[p]=0;
       for(int i=0;i<a[p].size();++i)
       {
           int q=a[p][i];
           if(dis[q]>dis[p]+w[p][q])//这里wa了一次,以后还是尽量别用min,because有后续语句的情况下不好操作;
           {
               dis[q]=dis[p]+w[p][q];
               if(!vis[q])
               {
                   vis[q]=1;
                   Q.push(q);
               }
           }
       }
   }
   return 0;
}

int main()
{
    int m,n;
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        memset(a,0,sizeof(a));
        memset(w,0x7f,sizeof(w));
        memset(vis,0,sizeof(vis));
        memset(dis,0x7f,sizeof(dis));
        dis[1]=0;
        for(int i=1;i<=n;++i)w[i][i]=0;
        for(int i=1;i<=m;++i)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            w[x][y]=w[y][x]=z;
            a[x].push_back(y);
            a[y].push_back(x);
        }
        spfa(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}


The end;

CF818G Four Melodies 题目描述 Author note: I think some of you might remember the problem “Two Melodies” from Eductational Codeforces Round 22. Now it’s time to make it a bit more difficult! Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. This time Alice wants to form four melodies for her tracks. Alice has a sheet with � n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7. You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. 输入格式 The first line contains one integer number � n ( 4 < = � < = 3000 4<=n<=3000 ). The second line contains � n integer numbers � 1 , � 2 , . . . , � � a 1 ​ ,a 2 ​ ,...,a n ​ ( 1 < = � � < = 10 5 1<=a i ​ <=10 5 ) — notes written on a sheet. 输出格式 Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. 输入输出样例 #1 输入 #1 5 1 3 5 7 9 输出 #1 4 输入输出样例 #2 输入 #2 5 1 3 5 7 2 输出 #2 5 说明/提示 In the first example it is possible to compose 4 4 melodies by choosing any 4 4 notes (and each melody will consist of only one note). In the second example it is possible to compose one melody with 2 2 notes — 1 , 2 1,2 . Remaining notes are used in other three melodies (one note per each melody). 代码: C++ #include <bits/stdc++.h> using namespace std; class MinCostMaxFlow { public: MinCostMaxFlow(int n, int s, int t) : n(n), s(s), t(t) { head.assign(n + 1, -1); } void add_edge(int u, int v, int w, int c) { e.push_back(Edge{v, w, c, head[u]}); head[u] = e.size() - 1; e.push_back(Edge{u, 0, -c, head[v]}); head[v] = e.size() - 1; } bool SPFA() { now.assign(head.begin(), head.end()); dis.assign(n + 1, INF); inq.assign(n + 1, false); queue<int> q; dis[s] = 0, q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int i = head[u]; ~i; i = e[i].nxt) { int v = e[i].to; if (e[i].w > 0 && dis[v] > dis[u] + e[i].c) { dis[v] = dis[u] + e[i].c; if (!inq[v]) q.push(v), inq[v] = true; } } } return dis[t] != INF; } int dfs(int u, int sum, int& cost) { if (u == t) return sum; int flow = 0; for (int i = head[u]; ~i && sum > 0; i = e[i].nxt) { int v = e[i].to; if (e[i].w > 0 && dis[v] == dis[u] + e[i].c) { int tmp = dfs(v, min(sum, e[i].w), cost); if (!tmp) dis[v] = INF; e[i].w -= tmp, e[i ^ 1].w += tmp; sum -= tmp, flow += tmp; cost += tmp * e[i].c; } } return flow; } pair<int, int> Dinic() { int maxflow = 0, mincost = 0; while (SPFA()) maxflow += dfs(s, INF, mincost); return make_pair(maxflow, mincost); } private: const int INF = 1e9; struct Edge {int to, w, c, nxt; }; int n, s, t; vector<Edge> e; vector<int> head, now, dis; vector<bool> inq; }; const int INF = 1e9; const int MAX_N = 3050; int n, a[MAX_N]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int s0 = n * 2 + 1, s = n * 2 + 2, t = n * 2 + 3; MinCostMaxFlow mcmf(n * 2 + 3, s0, t); mcmf.add_edge(s0, s, 4, 0); for (int i = 1; i <= n; i++) { mcmf.add_edge(s, i, INF, 0); mcmf.add_edge(i, i + n, 1, -1); mcmf.add_edge(i + n, t, INF, 0); for (int j = i + 1; j <= n; j++) if (a[i] % 7 == a[j] % 7) { mcmf.add_edge(i + n, j, INF, 0); mcmf.add_edge(i, j, INF, 0); break; } for (int j = i + 1; j <= n; j++) if (a[i] + 1 == a[j]) { mcmf.add_edge(i + n, j, INF, 0); break; } for (int j = i + 1; j <= n; j++) if (a[i] - 1 == a[j]) { mcmf.add_edge(i + n, j, INF, 0); break; } for (int j = i + 1; j <= n; j++) if (a[i] == a[j]) { mcmf.add_edge(i, j, INF, 0); break; } } auto ans = mcmf.Dinic(); cout << -ans.second << '\n'; return 0; } 为什么在第二个样例死循环了
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值