Let’s face it… you are not that handy. When you need to make a major home repair, you often need to hire someone to help. When they come for the first visit, they make an estimate of the cost. Here they must be careful: if they overestimate the cost, it might scare you off, but if they underestimate, the work might not be worth their time.
Because the worker is so careful, it can take a long time for them to produce the estimate. But that’s frustrating — when you ask for an estimate, you really are asking for the magnitude of the cost. Will this be $10
or $100 or $1000
? That’s all you really want to know on a first visit.
Please help the worker make the type of estimate you desire. Write a program that, given the worker’s estimate, reports just the magnitude of the cost — the number of digits needed to represent the estimate.
Input
Input begins with a line containing an integer N
(1≤N≤100). The next N lines each contain one estimated cost, which is an integer between 0 and 10100
. (Some of the workers overcharge quite a bit.)
Output
For each estimated cost, output the number of digits required to represent it.
#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
int n;
int val[maxn];
int mapp[maxn][maxn];
int dis[maxn],ans[maxn];
bool vis[maxn];
void dij()
{
for(int i=1; i<=n; i++)
dis[i]=mapp[1][i];
dis[1]=0;
int minn,V=-1,k;
for (int i = 1; i <=n ; ++i)
{
minn=inf;
k=V;
for(int j=1; j<=n; j++)
{
if(!vis[j]&&minn>dis[j])
{
minn=dis[j];
V=j;
}
}
vis[V]=true;
for (int j = 1; j <=n ; ++j)
{
if(!vis[j]&&dis[j]>dis[V]+mapp[V][j])
{
dis[j]=dis[V]+ mapp[V][j];
ans[j]=ans[V]+val[j];
}
else if(!vis[j]&&dis[j]==dis[V]+mapp[V][j])
{
ans[j]=max(ans[j],ans[V]+val[j]);
}
}
}
if(dis[n]==inf)
printf("impossible\n");
else
printf("%d %d\n",dis[n],ans[n]+val[1]);
}
int main()
{
scanf("%d",&n);
memset(mapp,0x3f, sizeof(mapp));
for (int i =1; i <=n ; ++i)
{
scanf("%d",&val[i]);
mapp[i][i]=0;
}
int m;
scanf("%d",&m);
int x,y,z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
mapp[x][y]=mapp[y][x]=z;
}
dij();
return 0;
}