我写的跟题解一模一样,但是没过
  • 板块灌水区
  • 楼主da_ke
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/9/17 20:14
  • 上次更新2024/9/18 07:14:42
查看原帖
我写的跟题解一模一样,但是没过
766675
da_ke楼主2024/9/17 20:14
#include <bits/stdc++.h>

#define i64 long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define fdn(i,r,l) for(int i=(r);i>=(l);i--)
#define pii pair<int,int>
using namespace std;

typedef long long ll;
typedef double db;
typedef __int128 i128;

const int N=1e4+23;
const int K=114;

int n,m,k;
ll dis[N][K]; bool vis[N][K];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("bus.in","r",stdin);
    freopen("bus.out","w",stdout);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    cin>>n>>m>>k;
    vector<vector<pair<int,int> > > linker(n+1);
    rep(i,1,m)
    {
        int u,v,w;
        cin>>u>>v>>w;
        linker[u].push_back({v,w});
    }
    priority_queue<pair<ll,int> > q;
    rep(i,1,n) rep(j,0,k-1) dis[i][j]=1ll<<59;
    dis[1][0]=0;
    q.push({0,1});
    while(!q.empty())
    {
        int u=q.top().second; ll p=q.top().first;
        q.pop();
        if(vis[u][p%k])
            continue;
        vis[u][p%k]=1;
        for(auto i:linker[u])
        {
            int v=i.first; ll w=i.second;
            ll t;
            if(p>=w) t=p;
            else t=ceil(1.0*(w-p)/k)*k+p;
            if(t+1<dis[v][(t+1)%k])
            {
                dis[v][(t+1)%k]=t+1;
                q.push({t+1,v});
            }
        }
    }
    cout<<((dis[n][0]==1ll<<59)?-1:dis[n][0])<<endl;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=10010,M=105;
inline ll read(){
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')
          f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    return x*f;
}
inline void write(ll x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)
	  write(x/10);
	putchar(x%10+'0');
}
ll n,m,k;
ll dis[N][M];
bool f[N][M];
vector<pair<ll,ll>> E[N];
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> q;
void add(ll u,ll v,ll w){
	E[u].push_back({v,w});
}
void dijkstra(ll s){
	dis[s][0]=0;
	q.push({0,s});
	while(!q.empty()){
		ll u=q.top().second,p=q.top().first;
		q.pop();
		if(f[u][p%k])
		  continue;
		f[u][p%k]=1;
		for(auto d:E[u]){
			ll v=d.first,w=d.second,t=(p+1)%k;
			if(p>=w)
			  t=p;
			else
			  t=((w-p+k-1)/k)*k+p;
			if(dis[v][(t+1)%k]>t+1){
				dis[v][(t+1)%k]=t+1;
				q.push({t+1,v});
			}
		}
	}
}
int main(){
	memset(dis,0x3f,sizeof(dis));
	n=read(),m=read(),k=read();
	for(int u,v,w,i=0;i<m;i++){
		u=read(),v=read(),w=read();
		add(u,v,w);
	}
	dijkstra(1);
	if(!f[n][0])
	  puts("-1");
	else
	  write(dis[n][0]);
	return 0;
}

题目是:P9751

下面的是题解,求对比!禁止讨论我为啥抄题解之类的内容,您得先对比出这两份代码有那些不同。

2024/9/17 20:14
加载中...