With dis [] to store this data: From the station i, requires a minimum of number from candy i can put all transport finished. This distance, in fact, just need to know where to find a candy can finally sent, before the candy is a way "to run a full circle" to the.
The final answer, just assume that s number from the station, then through each station, to find such a station i, from s to i, plus dis [i] is the largest, this is all the candy while stocks must run distance.
#include<bits/stdc++.h>
using namespace std;
struct node {int x,y;};
vector<int> s[101];//每个车站的糖果情况
int n,m;
int Min(vector<int> v,int s)//车站s中可以运送掉一个货物的最短距离
{
int m=0x3f3f3f3f;
for(int i=0;i<v.size();i++){
if(v[i]>s){
m=min(m,v[i]-s);
}
if(v[i]<=s) {
m=min(m,n-(s-v[i]));
}
}
return m;
}
int main()
{
cin>>n>>m;//m个货物
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
s[x].push_back(y);
}
//接下来记录距离
int dis[101];
memset(dis,0x3f,sizeof(dis));
//找到从每个车站出发需要的最短距离
for(int i=1;i<=n;i++)
{
if(s[i].empty()) continue;
else{
dis[i]=Min(s[i],i)+n*(s[i].size()-1);
}
}
// for(int i=1;i<=n;i++){
// cout<<dis[i]<<endl;
// }
for(int s1=1;s1<=n;s1++){
//每一个车站为起点
int m=0;
for(int i=1;i<=n;i++){
if(s[i].empty()) continue;
if(i<s1) m=max(m,dis[i]+n-(s1-i));//i在s后面,看做先跑一圈到s,再倒退回i
else m=max(m,dis[i]+i-s1);
}
cout<<m<<' ';
}
cout<<endl;
return 0;
}