写挂了24pts
查看原帖
写挂了24pts
408677
beifa楼主2023/7/27 11:35

挂掉了,样例都没过去。。。


(和题解除了变量名不一样其他基本一样)

#include <bits/stdc++.h>
#define int long long
using namespace std;

int d[3010] , dis[3010];
int n , m;
struct edge{
    int to,c;
};
vector<edge>g[3010];
vector<edge>G[3010];
typedef pair<int,int> p;
int cnt[3010] , vis[3010];

bool spfa()
{
    queue<int>q;
    memset(vis,0,sizeof vis);
    for(int i = 1 ; i <= n ; ++i) d[i] = 1e9;
    d[0] = 0;
    q.push(0);
    vis[0] = 1;
    cnt[0]++;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = 0 ; i < g[u].size() ; ++i)
        {
            edge e = g[u][i];
            if(d[e.to] > d[u]+e.c)
            {
                d[e.to] = d[u]+e.c;
                if(!vis[e.to])
                {
                    vis[e.to] = 1;
                    cnt[e.to]++;
                    q.push(e.to);
                    if(cnt[e.to] >= n+1) return 0;
                }
            }
        }
    }
    return 1;
}

void dij(int s)
{
    priority_queue<p,vector<p>,greater<p> >q;
    for(int i = 1 ; i <= n ; ++i){dis[i] = 1e9;vis[i] = 0;}
    memset(vis,0,sizeof vis);
    q.push({0,s});
    dis[s] = 0;
    while(!q.empty())
    {
        p now = q.top();
        q.pop();
        int u = now.second;
        if(dis[u] < now.first) continue;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = 0 ; i < g[u].size() ; ++i)
        {
            edge e = g[u][i];
            if(dis[e.to] > dis[u]+e.c)
            {
                dis[e.to] = dis[u]+e.c;
                if(!vis[e.to]) q.push({dis[e.to],e.to});
            }
        }
    }
}
int res[3010];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        int a , b , c;
        cin >> a >> b >> c;
        g[a].push_back({b,c});
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        g[0].push_back({i,0});
    }
    if(!spfa())
    {
        cout << -1;
        exit(0);
    }
    // for(int i = 1 ; i <= n ; ++i)
    // {
    //     cout << d[i] << " ";
    // }
    for(int i = 1 ; i <= n ; ++i)
    {
        for(int j = 0 ; j < g[i].size() ; ++j)
        {
            edge e = g[i][j];
            e.c += d[i] - d[e.to];
        }
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        dij(i);
        int ans = 0;
        for(int j = 1; j <= n ; ++j)
        {
            if(dis[j] == 1e9) ans+=1e9*j;
            else ans += j*(dis[j]+d[j]-d[i]);
        }
        cout << ans << '\n';
    }
    return 0;
}
2023/7/27 11:35
加载中...