1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 #include<vector>
5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
6 using namespace std;
7
8 typedef long long LL ;
9 const int maxn = 1000+10;
10 const int INF = 1e9;
11
12 struct Edge{ int u,v,cap,flow,cost;
13 };
14
15 struct MCMF {
16 int n,m,s,t;
17 int inq[maxn],a[maxn],d[maxn],p[maxn];
18 vector<int> G[maxn];
19 vector<Edge> es;
20
21 void init(int n) {
22 this->n=n;
23 es.clear();
24 for(int i=0;i<n;i++) G[i].clear();
25 }
26 void AddEdge(int u,int v,int cap,int cost) {
27 es.push_back((Edge){u,v,cap,0,cost});
28 es.push_back((Edge){v,u,0,0,-cost});
29 m=es.size();
30 G[u].push_back(m-2);
31 G[v].push_back(m-1);
32 }
33
34 bool SPFA(int s,int t,int& flow,LL& cost) {
35 for(int i=0;i<n;i++) d[i]=INF;
36 memset(inq,0,sizeof(inq));
37 d[s]=0; inq[s]=1; p[s]=0; a[s]=INF;
38 queue<int> q; q.push(s);
39 while(!q.empty()) {
40 int u=q.front(); q.pop(); inq[u]=0;
41 for(int i=0;i<G[u].size();i++) {
42 Edge& e=es[G[u][i]];
43 int v=e.v;
44 if(e.cap>e.flow && d[v]>d[u]+e.cost) {
45 d[v]=d[u]+e.cost;
46 p[v]=G[u][i];
47 a[v]=min(a[u],e.cap-e.flow); //min(a[u],..)
48 if(!inq[v]) { inq[v]=1; q.push(v);
49 }
50 }
51 }
52 }
53 if(d[t]==INF) return false;
54 flow+=a[t] , cost += (LL) a[t]*d[t];
55 for(int x=t; x!=s; x=es[p[x]].u) {
56 es[p[x]].flow+=a[t]; es[p[x]^1].flow-=a[t];
57 }
58 return true;
59 }
60 int Mincost(int s,int t,LL& cost) {
61 int flow=0; cost=0;
62 while(SPFA(s,t,flow,cost)) ;
63 return flow;
64 }
65 } mc;
66
67 int n;
68
69 int main() {
70 scanf("%d",&n);
71 mc.init(n+2);
72 int s=0,t=n+1;
73 mc.AddEdge(t,s,INF,0);
74 FOR(u,1,n) {
75 int m,v,c; scanf("%d",&m);
76 if(u!=1) mc.AddEdge(u,1,INF,0);
77 mc.AddEdge(u,t,m,0);
78 FOR(j,1,m) {
79 scanf("%d%d",&v,&c);
80 mc.AddEdge(u,v,INF,c);
81 mc.AddEdge(s,v,1,c);
82 }
83 }
84 LL cost;
85 mc.Mincost(s,t,cost);
86 printf("%lld\n",cost);
87 return 0;
88 }