Title:
1003: [ZJOI2006] Logistics Transportation
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 8000 Solved: 3343
[Submit][Status][Discuss]
Description
Logistics companies will ship a shipment of goods from Terminal A to Terminal B. Because of the large volume of goods, it takes n days to complete the shipment. In the course of cargo transportation, it is generally necessary to transfer the goods.
Stop at several docks. Logistics companies usually design a fixed transport route in order to implement strict management and tracking of the entire transport process. Because of all kinds
The existence of factors, sometimes a wharf will not be able to load and unload cargo. At this time, it is necessary to modify the transport route so that the goods can arrive at their destination on time. however
Modifying the route is a very cumbersome matter, which will bring additional costs. Therefore, logistics companies want to be able to make an n-day transportation plan to make the total cost.
As small as possible.
Input
The first line is four integers n (1<=n<=100), m (1<=m<=20), K and e. N denotes the number of days needed to transport goods, m denotes the total number of docks, and K denotes the total number of docks.
The cost of each modification of the transport route. Next line e is a description of each route, including three integers, which in turn represent the two docks connected by the route.
Number and route length (>0). The number of wharf A is 1 and the number of wharf B is m. The transportation cost per unit length is 1. The route is two-way. Next up
One line is a n integer d, and the following D lines are three integers P (1 < P < m), A and b (1 < = a < = b < = n). Represents a code numbered P
The cargo can not be loaded and unloaded from day a to day b. The same wharf may not be available for several periods of time. But at least one exists at any time.
The transportation route of the bar from wharf A to wharf B.
Output
Includes an integer representing the minimum total cost. Total cost = the sum of the length of transport routes in n days + the number of times K* changes in transport routes.
Sample Input
5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5
Sample Output
32
// Walk 1-4-5 in the first three days and 1-3-5 in the second two days. The total cost is (2+2)*3+(3+2)*2+10=32.
HINT
Source
[Submit][Status][Discuss]
thinking
Firstly, the following definitions are given:
- dp[i] represents the minimum cost from day one to day I
- flag[x][j] represents the current terminal x, can it be used on day J
-
t[i][j] represents the shortest path from day I to day j, from point 1 to point m
The idea is that we first find out all the shortest paths from day I to day J and store them in t[i][j]. Then we traverse all the days. First, we initialize dp[i] into the shortest path * unit cost from day 1 to day I.
Then the recursive formula is obtained.dp[i]=min(dp[i],dp[j]+t[j+1][i]*(i-j)+k)
t[j+1][i]*(i-j)+k is the cost of choosing the j+1 to I days, plus the cost of transferring routes.
Code:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define N 1010
#define M 12357
#define ll long long
using namespace std;
int n,m,k,e;
int vis[N],first[N],len;
int dis[N];
int flag[N][N];//flag[x][i] stands for point X that cannot be used on day I
int dp[N];//dp[i] represents the minimum cost from day 1 to day 1
int t[N][N];//t[i][j] denotes the shortest path from the first point to the m-th point from day I to day J
struct node
{
int u,v,w,next;
} G[N*N];
void add_edge(int u,int v,int w)
{
G[len].v=v,G[len].w=w;
G[len].next=first[u];
first[u]=len++;
}
int spfa(int st,int ed)
{
int book[N];
mem(dis,inf);
mem(book,0);
mem(vis,0);
for(int i=1; i<=m; i++)
for(int j=st; j<=ed; j++)
if(flag[i][j])
book[i]=1;//Point i Can't Go
dis[1]=0;
vis[1]=1;
queue<int>q;
q.push(1);
while(!q.empty())
{
st=q.front();
q.pop();
vis[st]=0;
for(int i=first[st]; i!=-1; i=G[i].next)
{
int v=G[i].v,w=G[i].w;
if(!book[v]&&dis[v]>dis[st]+w)
{
dis[v]=dis[st]+w;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return dis[m];
}
void solve()
{
for(int i=1;i<=n;i++)
{
dp[i]=t[1][i]*i;//Length * Expense per unit length = Expense
for(int j=0;j<i;j++)
dp[i]=min(dp[i],dp[j]+t[j+1][i]*(i-j)+k);
}
}
int main()
{
int x,y,z;
mem(first,-1);
mem(flag,0);
len=1;
scanf("%d%d%d%d",&n,&m,&k,&e);
for(int i=1; i<=e; i++)
{
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
add_edge(y,x,z);
}
int d,p,a,b;
scanf("%d",&d);
for(int i=1; i<=d; i++)
{
scanf("%d%d%d" ,&p,&a,&b);
for(int j=a; j<=b; j++)
flag[p][j]=1;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
t[i][j]=spfa(i,j);//Find the shortest path from 1 to m that meets the requirements from day i to day j
solve();
printf("%d\n",dp[n]);
return 0;
}