1018 . Public Bike Management (30)自己寫的,只有14分
阿新 • • 發佈:2019-02-04
#include<iostream>
#include<vector>
using namespace std;
class CA
{
public:
enum{C=100,N=501,INF=0x6FFFFFFF};
void run();
void shortest();
void dowell(int v);
private:
int c,n,dp,m;
int bikes[N];
int roads[N][N];
int d[N];
int send,back;
vector<int> posipath[N];
};
void CA::dowell(int v)
{
int i,j,tmp,k,l;
int path[N];
fill_n(path,n+1,-1);
i=v;
if(bikes[i]==0)
{
tmp=posipath[i][0];
for(j=0;j<posipath[i].size();j++)
{
if(bikes[tmp]<bikes[posipath[i][j]])
tmp=posipath[i][j];
}
send=c-bikes[tmp];
if(send>5)
send=5;
else
{
bikes[tmp]=c/2;
}
back=0;
printf("%d ",send);
k=1;
path[0]=i;
while(1)
{
path[k]=tmp;
if(posipath[tmp].size()==0)
break;
tmp=posipath[tmp][0];
k++;
}
//printf("%d\n",k);
printf("0");
for(l=k;l>=0;l--)
{
printf("->%d",path[l]);
}
printf(" 0");
}
else if(bikes[i]>=c)
{
tmp=posipath[i][0];
for(j=0;j<posipath[i].size();j++)
{
if(bikes[tmp]>bikes[posipath[i][j]])
tmp=posipath[i][j];
}
send=0;
if((bikes[tmp]+bikes[i]-c)<=c/2)
{
back=0;
bikes[tmp]=bikes[tmp]+bikes[i]-c;
}
else if((bikes[tmp]+bikes[i]-c)>c/2)
{
back=(bikes[tmp]+bikes[i]-c)-c/2;
bikes[tmp]=5;
}
printf("%d ",send);
k=1;
path[0]=i;
while(1)
{
path[k]=tmp;
if(posipath[tmp].size()==0)
break;
tmp=posipath[tmp][0];
k++;
}
//printf("%d\n",k);
printf("0");
for(l=k;l>=0;l--)
{
printf("->%d",path[l]);
}
printf(" %d",back);
}
}
void CA::shortest()
{
bool visited[N];
fill_n(visited,n+1,false);
int i;
for(i=1;i<n+1;i++)
d[i]=roads[0][i];
d[0]=0;
//visited[0]=true;
while(1)
{
int k=-1;
for(i=1;i<n+1;i++)
{
if(visited[i])
continue;
if(k==-1||d[i]<d[k])
k=i;
}
if(k==-1)
break;
visited[k]=true;
for(i=1;i<n+1;i++)
{
if(visited[i])
continue;
if(d[i]>d[k]+roads[k][i])
{
d[i]=d[k]+roads[k][i];
posipath[i].clear();
posipath[i].push_back(k);
}
else if(d[i]==d[k]+roads[k][i])
{
posipath[i].push_back(k);
}
}
}
}
void CA::run()
{
int i,v1,v2,w;
cin>>c>>n>>dp>>m;
fill_n((int*)roads,N*N,INF);
for(i=1;i<n+1;i++)
cin>>bikes[i];
bikes[0]=0;
for(i=0;i<m;i++)
{
cin>>v1>>v2>>w;
roads[v1][v2]=w;
roads[v2][v1]=w;
}
shortest();
dowell(dp);
}
int main()
{
//freopen("test2.txt","r",stdin);
CA *a=new CA;
a->run();
return 0;
}
#include<vector>
using namespace std;
class CA
{
public:
enum{C=100,N=501,INF=0x6FFFFFFF};
void run();
void shortest();
void dowell(int v);
private:
int c,n,dp,m;
int bikes[N];
int roads[N][N];
int d[N];
int send,back;
vector<int> posipath[N];
};
void CA::dowell(int v)
{
int i,j,tmp,k,l;
int path[N];
fill_n(path,n+1,-1);
i=v;
if(bikes[i]==0)
{
tmp=posipath[i][0];
for(j=0;j<posipath[i].size();j++)
{
if(bikes[tmp]<bikes[posipath[i][j]])
tmp=posipath[i][j];
}
send=c-bikes[tmp];
if(send>5)
send=5;
else
{
bikes[tmp]=c/2;
}
back=0;
printf("%d ",send);
k=1;
path[0]=i;
while(1)
{
path[k]=tmp;
if(posipath[tmp].size()==0)
break;
tmp=posipath[tmp][0];
k++;
}
//printf("%d\n",k);
printf("0");
for(l=k;l>=0;l--)
{
printf("->%d",path[l]);
}
printf(" 0");
}
else if(bikes[i]>=c)
{
tmp=posipath[i][0];
for(j=0;j<posipath[i].size();j++)
{
if(bikes[tmp]>bikes[posipath[i][j]])
tmp=posipath[i][j];
}
send=0;
if((bikes[tmp]+bikes[i]-c)<=c/2)
{
back=0;
bikes[tmp]=bikes[tmp]+bikes[i]-c;
}
else if((bikes[tmp]+bikes[i]-c)>c/2)
{
back=(bikes[tmp]+bikes[i]-c)-c/2;
bikes[tmp]=5;
}
printf("%d ",send);
k=1;
path[0]=i;
while(1)
{
path[k]=tmp;
if(posipath[tmp].size()==0)
break;
tmp=posipath[tmp][0];
k++;
}
//printf("%d\n",k);
printf("0");
for(l=k;l>=0;l--)
{
printf("->%d",path[l]);
}
printf(" %d",back);
}
}
void CA::shortest()
{
bool visited[N];
fill_n(visited,n+1,false);
int i;
for(i=1;i<n+1;i++)
d[i]=roads[0][i];
d[0]=0;
//visited[0]=true;
while(1)
{
int k=-1;
for(i=1;i<n+1;i++)
{
if(visited[i])
continue;
if(k==-1||d[i]<d[k])
k=i;
}
if(k==-1)
break;
visited[k]=true;
for(i=1;i<n+1;i++)
{
if(visited[i])
continue;
if(d[i]>d[k]+roads[k][i])
{
d[i]=d[k]+roads[k][i];
posipath[i].clear();
posipath[i].push_back(k);
}
else if(d[i]==d[k]+roads[k][i])
{
posipath[i].push_back(k);
}
}
}
}
void CA::run()
{
int i,v1,v2,w;
cin>>c>>n>>dp>>m;
fill_n((int*)roads,N*N,INF);
for(i=1;i<n+1;i++)
cin>>bikes[i];
bikes[0]=0;
for(i=0;i<m;i++)
{
cin>>v1>>v2>>w;
roads[v1][v2]=w;
roads[v2][v1]=w;
}
shortest();
dowell(dp);
}
int main()
{
//freopen("test2.txt","r",stdin);
CA *a=new CA;
a->run();
return 0;
}